only getting string "OK" instead of the device/registration id from cordova push plugin using Android

2.8k views Asked by At

I've set up the push notification plugin and am calling the register method, but it's only returning a string "OK" instead of a device id. How do I get the registered device id?

   $window.plugins.pushNotification.register(
          function (result) {
            q.resolve(result); //this always just returns the string "OK", how do I get the device ID?
          },
          function (error) {
            console.log(error);
            q.reject(error);
          },
          config);

        return q.promise;
      },

e.regid is null, taken from this example

// Android and Amazon Fire OS
function onNotification(e) {
    $("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');

    switch( e.event )
    {
    case 'registered':
        if ( e.regid.length > 0 )
        {
            $("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");
            // Your GCM push server needs to know the regID before it can push to this device
            // here is where you might want to send it the regID for later use.
            console.log("regID = " + e.regid);
        }
    break;

    case 'message':
        // if this flag is set, this noti
3

There are 3 answers

5
MonkeyBonkey On BEST ANSWER

so I just mis-understood the callbacks in the push library. The register function callback on iOS returns a string token, wheras in Android it just returns a success handler. The registration event's token id in the Android version has to be handled on the push notification ecb handler rather than in the registration callback.

So in their example

if ( device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos" ){
    pushNotification.register(
    successHandler,
    errorHandler,
    {
        "senderID":"replace_with_sender_id",
        "ecb":"onNotification"
    });
} 

the function is set in the config property "ecb" as "onNotification" and not in the sucess handler.

In which case you can use the onNotification example in the original question. My mistake was passing the onNotification function as the success handler in Android, which is not the case.

0
laughingpine On

My mistake regarding the registration was using the incorrect project id. At first I was using the unique name (also known as project id in the dev console) of the project instead of the project number. The callback is successful regardless of gcm's response.

I was also getting a similar problem to this on both of my Samsung test devices: the register callback was working fine, but the application was not getting the push notifications. This seemed to be related to some of the services on my test devices being out of date.

Finally, be sure to include a 'message' and 'title' in the payload data if you want the notification to display properly in the system tray.

0
José Antonio Postigo On

In android the device token can be got from the ecb callback when the device registration event is triggered. This code ilustrates how I do it (it could be more organized but for our purpose it is enought):

var GOOGLE_SENDER_ID = '/* replace with yours */';
var deferred;

function registerWithGCMServer() {
    deferred = $.Deferred();

    window.plugins.pushNotification.register(
        function() {
            //It will be resolved in 'window.onAndroidNotification'
            //when the device is registered and the token is gotten
            //our rejected if the timeout is reached
        },
        function() {
            deferred.reject('Error registering.');
        }, {
            senderID: GOOGLE_SENDER_ID,
            ecb: 'window.onAndroidNotification'
        });

    setTimeout(function() {
        if(deferred.state() === 'pending') {
            deferred.reject('Error registering (timeout).');
        }
    }, 10000); //10s

    return deferred.promise();
}

window.onAndroidNotification = function(e) {
    if(e.event == 'registered') {
        deferred.resolve(e.regid);
    } else if(e.event == 'message') {
        onMessageRecived.call(null, e.message);
    }
};

And then use its functionality:

registerWithGCMServer()
    .then(function(deviceToken) {
        console.log('Device registered and its token is ' + deviceToken);
    })
    .fail(function(e) {
        console.error(e);
    });

function onMessageRecived(message) {
    console.log('Push message received: ' + message);
}