After searching all the questions in stackoverflow using Cordova / Phonegap Push Plugin, I found the source of the problem. It is written to the source code of the plugin, and it should not.
The problem was that the source code of the plugin requires the push notification to have a “message” field in order to trigger the notification while in the background. It is specified in the GCMIntentService.java file.
if (extras.getString("message") != null && extras.getString("message").length() != 0) { createNotification(context, extras); }
The most useful are the posts and issue in the plugin repository. Therefore, since I do not have authority over the action of the push server, I cannot specify the "message" field in push notifications, I had no alternative but to rewrite the source code.
So, I rewrote the te code as follows:
@Override protected void onMessage(Context context, Intent intent) { Log.d(TAG, "onMessage - context: " + context); // Extract the payload from the message Bundle extras = intent.getExtras(); if (extras != null) { // if we are in the foreground, just surface the payload, else post it to the statusbar if (PushPlugin.isInForeground()) { extras.putBoolean("foreground", true); PushPlugin.sendExtras(extras); } else { extras.putBoolean("foreground", false); // Send a notification if there is a message //if (extras.getString("message") != null && extras.getString("message").length() != 0) { if (extras.getString("alert") != null && extras.getString("alert").length() != 0) { createNotification(context, extras); } } } }
Thus, the plugin triggers a local notification if it receives push notifications with the field "alert" instead of "message".
Then I changed the code for local notification:
public void createNotification(Context context, Bundle extras) { NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); String appName = getAppName(this); Intent notificationIntent = new Intent(this, PushHandlerActivity.class); notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); notificationIntent.putExtra("pushBundle", extras); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); int defaults = Notification.DEFAULT_ALL; if (extras.getString("defaults") != null) { try { defaults = Integer.parseInt(extras.getString("defaults")); } catch (NumberFormatException e) {} } NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setDefaults(defaults) .setSmallIcon(context.getApplicationInfo().icon) .setWhen(System.currentTimeMillis()) //.setContentTitle(extras.getString("title")) .setContentTitle(extras.getString("alert")) //.setTicker(extras.getString("title")) .setTicker(extras.getString("alert")) .setContentIntent(contentIntent) .setAutoCancel(true); /* String message = extras.getString("message"); if (message != null) { mBuilder.setContentText(message); } else { mBuilder.setContentText("<missing message content>"); } */ // Agregado mensaje predefinido mBuilder.setContentText("Haz clic para más información"); String msgcnt = extras.getString("msgcnt"); if (msgcnt != null) { mBuilder.setNumber(Integer.parseInt(msgcnt)); } int notId = 0; try { notId = Integer.parseInt(extras.getString("notId")); } catch(NumberFormatException e) { Log.e(TAG, "Number format exception - Error parsing Notification ID: " + e.getMessage()); } catch(Exception e) { Log.e(TAG, "Number format exception - Error parsing Notification ID" + e.getMessage()); } mNotificationManager.notify((String) appName, notId, mBuilder.build()); }
Now the notification in the notification panel will first display the "alert" field instead of the "title" and the second predefined message instead of the "message" field, which does not exist in my push notifications.
Then I just recompile the code with:
ionic platform rm android ionic platform add android ionic run android
So, children, this is what happens when you write a plugin for everyone, but you do not consider common cases, and you do not document well. This plugin is only useful for people with a message and title field in their push notifications.
I lost two days of my internship because of this, I spent my time writing this so that others would not have to.