Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Multiple Phonegap Push Notifications in Android’s Status Bar

DZone's Guide to

Multiple Phonegap Push Notifications in Android’s Status Bar

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

Last month, I worked within an Android project using Phonegap, jQuery Mobile and Push Notifications. I also wrote one post explaining how to use PHP to send the server side’s part of the push notifications. Today, I want to show one small hack that I’ve done to change the default behavior of push notifications. Let me explain it a little bit:

When you use the push plugin “out of the box,” you will see one message in your Android’s status bar every time you send a push notification (if your application isn't running at the moment). If you click on the notification, your application will start and you can handle this notification. But if you send more than one notification to the device, only the last one will be shown on the status bar. This behavior can be suitable for most situations, but within my application, I wanted to see all the notifications in the status bar until I clicked on one (then all had to disappear). If we want to do that, we need to hack a little bit in our Phonegap application. Let me show you what I’ve done.

Basically, we need to change the com.plugin.gcm.GCMIntentService file. If we open this Java file, we can see that there’s one constant called NOTIFICATION_ID and a public function called createNotification with something like this:

public static final int NOTIFICATION_ID = 237;
...
 
public void createNotification(Context context, Bundle extras)
{
    ...
    mNotificationManager.notify((String) appName, NOTIFICATION_ID, mBuilder.build());
 
}

I’m not a Java expert, but I notice that if I change this function to:

public static final int NOTIFICATION_ID = 237;
public static int MY_NOTIFICATION_ID = 237;
...
 
public void createNotification(Context context, Bundle extras)
{
    ...
    MY_NOTIFICATION_ID++;
    mNotificationManager.notify((String) appName, MY_NOTIFICATION_ID, mBuilder.build());
 
}

Now my android device will show multiple notifications, exactly as I need.

If we need to properly handle the way that our notifications are cancelled, we also need to modify the public function cancelNotification.

public static void cancelNotification(Context context)
{
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    //mNotificationManager.cancel((String)getAppName(context), MY_NOTIFICATION_ID);
    mNotificationManager.cancelAll();
    MY_NOTIFICATION_ID = NOTIFICATION_ID;
}

And that’s all. Multiple notifications as I needed.


Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Gonzalo Ayuso, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}