简体   繁体   中英

old push notifications not appearing on android device

I am trying to integrate push notifications for my andriod app using phonegap plugin and GCM(Google Cloud Messaging) service. I have basically followed all the steps from this tutorial on the client end, http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/ to register the device thru phonegap plugin.

  1. I have added phonegap plugin thru git phonegap local plugin as in "Part 1: Configure your app to use the PushPlugin" of the above tutorial.

  2. In the index.js file, I have added the following methods as mentioned in the "Part 2: Register the application with the Google Cloud Messaging service" of the above tutorial.

      successHandler: function(result) { alert('Callback Success! Result = '+result) }, errorHandler:function(error) { alert(error); }, onNotificationGCM: function(e) { switch( e.event ) { case 'registered': if ( e.regid.length > 0 ) { console.log("Regid " + e.regid); alert('registration id = '+e.regid); } break; case 'message': // this is the actual push notification. its format depends on the data model from the push server alert('message = '+e.message+' msgcnt = '+e.msgcnt); break; case 'error': alert('GCM error = '+e.msg); break; default: alert('An unknown GCM event has occurred'); break; } } 

To send the notification from the application server, I am using HTTP POST to https://android.googleapis.com/gcm/send with the request body in json format.

{
    “registration_ids” : ["APA....."],
    “data” : {
           “title” : “My Title”,
           “message” : “My Msg”,
           “msgcnt” : “1"
    }
}

I am receiving the notifications correctly on my device. I am not sure of what I am missing in the code, but I am running into these issues.

  1. In the notifications area on my device, I only see the last message when multiple messages are sent one after the another. It looks like the last message overrides the old messages. I want all the alerts to be listed on the device one after another as they get sent.

  2. Notifications are not appearing on the screen top when the device is locked.

How do I resolve these problems? I am new to mobile development, I would really appreciate your help on this. Thanks in advance!

For Getting the notifications when device is locked.

public abstract class WakeLocker { private static PowerManager.WakeLock wakeLock;

    public static void acquire(Context context) {
        if (wakeLock != null)
            wakeLock.release();

        PowerManager pm = (PowerManager) context
                .getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                | PowerManager.ACQUIRE_CAUSES_WAKEUP
                | PowerManager.ON_AFTER_RELEASE, "WakeLock");
        wakeLock.acquire();
    }

    public static void release() {
        if (wakeLock != null)
            wakeLock.release();
        wakeLock = null;
    }
}

also add following permission to your Manifest.

 <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 

The new notification overrides the old one probably because you use the same id in the notify method. Use a unique id and you'll get multiple notifications.

As for the second question, read this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM