Question

I am working on a Cordova Android app that handles push notifications using the PushPlugin. The app will correctly receive push notifications when the app is open. However no push notifications are received (or at least handled) if the app is not open.

This makes some sense given that the plugin has code that specifically handles receiving push notifications. What does not make much sense to me is why this code even exists at all. Before building this app, I assumed that the OS had some sort of queuing system that received any/all notifications.

  • Does a notification queue exists in Android that handles push notifications for all apps or does each app need to handle the notifications on their own.
  • Can Android apps process push notifications while closed?
  • Can Cordova Android apps process push notifications while closed?
Was it helpful?

Solution

  1. In Google Cloud Messaging (which is the push notifications mechanism used by the PushPlugin) each application decides how to handle the notification. When a push notification arrives to the device, GCM triggers a BroadcastReceiver in the recipient application, and the application is responsible for handling the notification data.

  2. Android apps can process push notifications when closed unless they are explicitly stopped by the user (in which case no notifications will reach the app until it is restarted again by the user). If they leave the foreground by the user switching to another app or going to the home screen, the can still receive and process notifications.

  3. Yes. According to the PushPlugin you posted a link to, it seems that they can. When the app is not in the foreground, the notification is handled differently - a notification is added to the notification bar, and when it is tapped, it should open the app.

OTHER TIPS

With PushPlugin, when sending push notifications to GCM (for GCM to send them to the device) one has to include a msgcnt key value pair after the message key value pair in the payload value in the JSON message, like this:

{
 "GCM":"{\"data\":{\"message\":\"hello\",\"msgcnt\":\"1\"}}"
}

or else PushPlugin will not handle the notification when the app is in the background. It has to do with the way the GCMIntentService.java file is written in the plugin.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top