简体   繁体   中英

android push notification in phone gap not getting notifications

Hi I follow the steps below

1) I created the cordova project structure.
2) I added the platform( android).
3) I added the cordova plugin

cordova plugin add https://github.com/phonegap-build/PushPlugin.git#2.4.0

4) Bulid the cordova project.
5) Next I import the created app in android eclipse(4.4.2)
6) I wrote the code below in index.js file

 init: function(){
     alert("init");
    var pushNotification = window.plugins.pushNotification;

    pushNotification.register(successHandler, errorHandler, 
        {
            'senderID':'XXXXXXXXXX',
            'ecb':'onNotificationGCM' // callback function
        }
    );

    function successHandler(result) {
        console.log('Success: '+ result);
        alert(result);
    }

    function errorHandler(error) {
        console.log('Error: '+ error);
    }

     function onNotificationGCM(e) {
        alert("comming");
        if('registered' === e.event) {
            // Successfully registered device.
        }
        else if('error' === e.event) {
            // Failed to register device.
        }
    };

I am getting the respose as "OK".and i am not able call 'ecb': onNotificationGCM' // callback function

In Android console I am getting the bellow Message

V/PushPlugin(2512): execute: action=register 
V/PushPlugin(2512): execute: data=     [{"senderID":"889953963751","ecb":"onNotificationGCM"}] V/PushPlugin(2512): execute: jo={"senderID":"889953963751","ecb":"onNotificationGCM"} V/PushPlugin(2512): execute: ECB=onNotificationGCM senderID=889953963751
    09-12 03:13:33.453: D/GCMRegistrar(2512): resetting backoff for com.ensis.hello
    09-12 03:13:33.613: V/GCMRegistrar(2512): Registering app com.ensis.hello of senders 889953963751
    09-12  W/PluginManager(2512): THREAD WARNING: exec() call to PushPlugin.register blocked the main thread for 181ms. Plugin should use CordovaInterface.getThreadPool().

This is the push notification flow:

  1. your app request a registration to the remote Apple or Google server
  2. if the registration is ok, the remove server returns a token, that identify this specific app on your device
  3. you send this token to your server, saving it (eg on a db)
  4. you send a push notification (from your server) calling Apple or Google services with the message and the tokens of the users you want to notify
  5. these services push to your device/app a notification with the message

You must follow all these steps in order to have push notification working.

For android you need to catch the registration id (token) inside the registered event of the notification handler:

function onNotificationGCM(e) {
    alert("comming");
    if('registered' === e.event) {
        // Successfully registered device.
        console.log("regID = " + e.regid);
        // save/send this registration id on your server
    } else if('error' === e.event) {
        // Failed to register device.
    }
};

For iOS you need to catch it in the succesHandler of the register function.

For more information look at this example in the plugin repository .

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