Patel
Patel

Reputation: 543

android GCM push notifications

I have successfully set up the code to run GCM over phonegap on an android app. I have managed to secure the handset registration ID and able to send a message to the app using this ID in a PHP script. My only problem is that the message displays as a javascript alert whilst the app is open, and I am looking to have message sent to the handset's but not showing icon on top notifications bar .

**> does is possible showing icon on top bar in android device

Does anyone know if the GCM plugin for Phonegap is capable of doing this?**

I have using C2DM-Phonegap. https://github.com/marknutter/GCM-Cordova

please help me friends...

Upvotes: 3

Views: 1630

Answers (4)

user3076732
user3076732

Reputation: 88

The library you are using specifies what needs to be done when a message is received. You can either modify their code to do what you want or roll your own GCMIntentService.

please refer this links :

https://github.com/phonegap-build/PushPlugin

https://build.phonegap.com/plugins/324

Upvotes: 4

jigspatel
jigspatel

Reputation: 410

please check this code

private static void generateNotification(Context context, String message) { int icon = R.drawable.ic_launcher;

       // here is your icon drawable instead of ic_launcher

    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context.getApplicationContext(), devicephp.class);
    // set intent so it does not start a new activity

    notificationIntent.putExtra("msg", message);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);


}

Upvotes: 0

cosmicnag
cosmicnag

Reputation: 103

The quickest way is to add the following code to GCMIntentService.java into the onMessage function. The GCM plugin you are using only receives the GCM message, but you have to trigger notifications on your own. You can also try a cordova plugin for status bar notifications.

 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) {
    String message = extras.getString("message");
    String title = extras.getString("title");

    Notification notif = new Notification(R.drawable.ic_launcher, message, System.currentTimeMillis() );
    notif.flags = Notification.FLAG_AUTO_CANCEL;
    notif.defaults |= Notification.DEFAULT_SOUND;
    notif.defaults |= Notification.DEFAULT_VIBRATE;

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

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

Upvotes: 0

toadzky
toadzky

Reputation: 3846

The library you are using specifies what needs to be done when a message is received. You can either modify their code to do what you want or roll your own GCMIntentService.

Upvotes: 2

Related Questions