How to handle push notification when application resumes?

Attempted to handle push notification using PushPlugin . Below is my code.

onNotificationGCM: function(e) { switch( e.event ) { case 'registered': if ( e.regid.length > 0 ) { console.log("Regid " + e.regid); //alert('registration id = '+e.regid); sDeviceId = e.regid; //alert(sDeviceId); } break; case 'message': // this is the actual push notification. its format depends on the data model from the push server alert('message = '+e.message); alert('message = '+e.message+' msgcnt = '+e.msgcnt); if ( e.foreground ) { alert("Notification Received"); } else { // otherwise we were launched because the user touched a notification in the notification tray. if ( e.coldstart ) { alert("coldstart"); } else { alert("other than coldstart"); } } break; case 'error': alert('GCM error = '+e.msg); break; default: alert('An unknown GCM event has occurred'); break; } } 

So everything works.

  • When the application is in the foreground, I get a warning.

  • when I click a notification, when a message received by the application opens, and I get a warning. (coldstart)

  • when the application is in the background, and then click the notification, the application comes to the fore and I get a warning.

But when I keep the application in the background and when a push notification arrives without clicking on the notification, when I bring the application to the foreground, I do not receive a warning. So how to deal with this situation?

+5
source share
2 answers

I ran into the same problem. Simply put, PushPlugin does not support this feature. But you can easily change it to suit your needs.

How does it work now

When a message is received by the GCMIntentService plugin, GCMIntentService is called . This method retrieves all the additional parameters passed to it, storing them in extras . After that, if the application is in the foreground, this function simply passes extras you, calling sendExtras . Otherwise, it calls createNotification , which actually creates a notification in the status bar.

Your problem

From what I understood from your question, a warning is not received if, after receiving a notification in the status bar, you open the application without touching the notification.

Here's what happens:

  • GCMIntentService receives a message
  • Since your application is in background , PushPlugin calls createNotification
  • When you start the application, it does not receive a warning, because you have not yet touched the notification in the status bar

You would receive a notification if you touched the notification because the intent of the notification has awakened your application. When your application wakes up, it searches for cached extra ones and then transfers them to your application, again calling sendExtras .

Decision

I have not tested this yet, but I strongly believe that if you edit your plugin, before sendExtras before (or after) you call createNotification , the data will be cached for your application, regardless of whether to notify or delete it.

 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) { createNotification(context, extras); } } } } 

Change the section above:

 protected void onMessage(Context context, Intent intent) { Log.d(TAG, "onMessage - context: " + context); Bundle extras = intent.getExtras(); if (extras != null) { if (PushPlugin.isInForeground()) { extras.putBoolean("foreground", true); } else { extras.putBoolean("foreground", false); if (extras.getString("message") != null && extras.getString("message").length() != 0) { createNotification(context, extras); } } // call sendExtras always PushPlugin.sendExtras(extras); } } 
+4
source

When the application is in the background, perhaps you need to save the boot information for notifications in localStorage , and then in the resume event of the application, read it from localStorage and show a warning message.

Here is a link with a similar problem

+1
source

Source: https://habr.com/ru/post/1215893/


All Articles