Push notification using PushPlugin returns OK and not reg id


#1

Push notification onNotificationGCM is not being fired and it returns OK instead of a registration id below is my code:

                // Push notification
            var androidConfig = {
                "senderID":"24125035000",
                "ecb" : "window.onNotificationGCM"
            };

            var iosConfig = {
                "badge":"true",
                "sound":"true",
                "alert":"true",
                "ecb" : "window.onNotificationAPN"
            };

            var pushNotification = window.plugins.pushNotification;
            if(device.platform == 'Android'){
                pushNotification.register(successHandler, errorHandler,androidConfig);
            }
            if(device.platform == 'iOS'){
                pushNotification.register(tokenHandler, errorHandler,iosConfig);
            }

            window.onNotificationAPN = function(e) {
                if (e.alert) {
                    navigator.notification.alert(e.alert);
                }
                if (e.sound) {
                    var snd = new Media(e.sound);
                    snd.play();
                }
                if (e.badge) {
                    pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
                }
            }
            // handle GCM notifications for Android

            window.onNotificationGCM = function(e) {
                switch (e.event) {
                    case 'registered':
                        if (e.regid.length > 0) {
                            navigator.notification.alert(e.regid);
                            // 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.
                            //$("#app-status-ul").append("<li>regID = " + e.regid +"</li>");
                            //alert(e.regid);
                            sessionStorage.setItem("deviceId",e.regid);
                        }
                        break;
                    case 'message':
                        // if this flag is set, this notification happened while we were in the foreground.
                        // you might want to play a sound to get the user's attention, throw up a dialog, etc.
                        if (e.foreground) {
                            navigator.notification.alert('--INLINE NOTIFICATION--');
                            // if the notification contains a soundname, play it.
                            var my_media = new Media("/android_asset/www/" + e.soundname);
                            my_media.play();
                        } else { // otherwise we were launched because the user touched a notification in the notification tray.
                            if (e.coldstart) navigator.notification.alert('--COLDSTART NOTIFICATION--');
                            else navigator.notification.alert('--BACKGROUND NOTIFICATION--');
                        }
                        navigator.notification.alert(e.payload.message);
                        navigator.notification.alert('MESSAGE -> MSGCNT: ' + e.payload.msgcnt);
                        break;
                    case 'error':
                        navigator.notification.alert('ERROR -> MSG:' + e.msg);
                        break;
                    default:
                        navigator.notification.alert('EVENT -> Unknown, an event was received and we do not know what it is');
                        break;
                }
            }
            function tokenHandler(result) {
                navigator.notification.alert(result, null, 'Alert', 'OK');
                sessionStorage.setItem("deviceId", result);
                sessionStorage.setItem("notificationServer", "APNS");
                // Your iOS push server needs to know the token before it can push to this device
                // here is where you might want to send it the token for later use.
            }

            function successHandler(result) {
                navigator.notification.alert(result, null, 'Alert', 'OK');
                sessionStorage.setItem("deviceId", result);
                sessionStorage.setItem("notificationServer", "GCM");
            }

            function errorHandler(error) {
                navigator.notification.alert(error, null, 'Alert', 'OK');
                //alert(error);
            }

I have checked the project number through my Google console and I am testing it on a Samsung Galaxy S4 with Android 4.4.4

Could you please tell me what I am doing wrong?


#2

did you find a solution? Im having the same problem


#3

Hi @tobbe, I managed to find a solution. I did 2 things :

  • Added the following lines to my main config.xml

        <feature name="PushPlugin">
        <param name="android-package" value="com.plugin.gcm.PushPlugin" />
        </feature>
      < plugin name="PushPlugin" value="com.plugin.gcm.PushPlugin" />
    

it appears the entry is not added to main config file. Try it and let me know if it works. :slight_smile:


#4

I found the solution to this

Apparently, installing the push plugin does not update the top level config.xml, so you need to add the following to the config.xml

  <feature name="PushPlugin">
 <param name="android-package" value="com.plugin.gcm.PushPlugin" />
 </feature>
< plugin name="PushPlugin" value="com.plugin.gcm.PushPlugin" />

I hope this helps someone else. :wink:


#5

hey, thanks for the help. Tried to add what you said but got the same “error”. I figured out though, that you get the Android push-token in the pushNotificationReceived and not the successHandler of $cordovaPush.register.


#6

Yes, same here! But why?! It doesn’t make sense to me when you read the docs.


#7

Nope, the docs seems to be insufficient in this case. I do it like this:

$rootScope.$on('pushNotificationReceived', function (event, resp) {
    var notification = resp.notification;

    console.log("PUSH RECEIVED: (" + notification.event + ")", event, resp);

    if (notification.event == "registered") {
        //console.log("notification:", notification.regid);
    }
    
});

$cordovaPush.register("things").then(function (token) {
        // if device is iOS you will get token here, if Android you'll get it in pushNotificationReceived event
       console.log(token);
    }, function (err) {
        console.log(err);
    });

Push Notification: Registration Returns OK but Never Received Registration ID ($cordovaPush) [SOLVED]
Cordova push
#8

I got an error when I used your code:

TypeError: Cannot read property ‘event’ of undefined

which points to:

console.log("PUSH RECEIVED: (" + notification.event + ")", event, resp);

I have to change it that way to get it work:

$rootScope.$on('pushNotificationReceived', function (event, notification) {
 // var notification = resp.notification;

 console.log("PUSH RECEIVED: (" + notification.event + ")", event, notification);

 if (notification.event == "registered") {
   console.log("notification:", notification.regid);
 }

});

#9

Ops, yea. I think I’ve poked in the ngCordova source code so I have to do that.


#10

Is there a full example?


#11

I always get successHandler to be Ok, but have ecb event.regid returns the right token id. Though it does not sound like default behaviour of PushPlugin (could be the deprecated GCMRegistrar class in the plugin), it does its job for now.


#12

Hi ionic friends, I also tried to get the push plugin running, but the ecb method ist not called in my controller. I don’t know why, so is it possible that anyone can build an example an post it on github. It would be really great and also help many people. thx


#13

Check if you use the right senderID. You have to use the ProjectID! See this post on stackoverflow.

This code works fine:

} else if(ionic.Platform.isAndroid()) {
      $ionicPlatform.ready(function () {
        $rootScope.$on('$cordovaPush:notificationReceived', function (event, notification) {
         // var notification = resp.notification;

         console.log("PUSH RECEIVED: (" + notification.event + ")", event, notification);

         if (notification.event == "registered") {
           console.log("notification:", notification.regid);

           UserService.registerDevice({
             user: user,
             token: notification.regid
           }).then(function () {
             $ionicLoading.hide();
             $state.go('tab.news');
           }, function (err) {
             console.log(err);
           });
         }

        });

        var config = {
          "senderID": "123456789012" // 12 digit project id
        };
        $cordovaPush.register(config).then(function(result) {
          // nothing to do here ;)
        }, function(err) {
          console.log('reg device error', err);
        });
      });
    }

#14

success retrieve token. error came from ionic-push cannot get gcm_key from ionic-core
modify some code inside /www/lib/ionic-service-push/ionic-push.js line 60, change the senderID value to your gcm_key (google api project number)

//Default configuration var config = { "senderID": '9000000000', // "senderID": gcmKey, "badge": true, "sound": true, "alert": true };