android phonegap
android phonegap

Reputation: 830

How to display a push notification for android in status bar using phonegap?

I want to implement push notification in android using phonegap and i used th ebelow link to implement GCM

https://github.com/marknutter/GCM-Cordova

I am able to implement push notification and i am able to get a small sample text on my mobile screen after submitting the appid regid and sender id but i want to display the notification in status bar. Can anyone please help me how to display a push notification in status bar using above example.

Upvotes: 2

Views: 2895

Answers (1)

Akshay Gawde
Akshay Gawde

Reputation: 183

You could use the Cordova StatusBarNotification plugin or if you want a faster solution, you could simply add the following native Java code into your GCMIntentService.java onMessage() function:

String message = extras.getString("message");
String title = extras.getString("title");
Notification notif = new Notification(android.R.drawable.btn_star_big_on, message, System.currentTimeMillis() );
notif.flags = Notification.FLAG_AUTO_CANCEL;
notif.defaults |= Notification.DEFAULT_SOUND;
notif.defaults |= Notification.DEFAULT_VIBRATE;

Intent notificationIntent = new Intent(context, TestSampleApp.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

notif.setLatestEventInfo(context, title, message, contentIntent);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
mNotificationManager.notify(1, notif);

and dont forget to add following imports,

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;

Be sure to replace YourActivityClassName.class with the name of your stub class that extends DroidGap. For instance, in the sample project it is called MainActivity.

Upvotes: 2

Related Questions