Skip to content Skip to sidebar Skip to footer

Ionic Push Notifications On Android Doesn't Register Token On Ionic.io

I want to implement push notification on Android using ionic. I've followed the documentation from Ionic push in a tutorial from devdactic Devdactic push notifications android I

Solution 1:

this is what I use to register pushes, it is messy but hopefully can be of some use. It checks if the current user is authenticated, if they aren't then it signs them up with a UUID (i used a UUID generator plugin) and saves the token. Just make sure your app is set up with Ionic.io and this should work :)

var user = Ionic.User.current();

if (user.isAuthenticated()) {

            var push = newIonic.Push({
                "debug": true,
                "onNotification": function (notification) {                        
                },
                "onRegister": function (data) {
                    console.log(data.token);
                    returntrue;
                },
                "pluginConfig": {
                    "android": {
                        "icon": "icon"
                    },
                    "ios": {
                        "badge": true,
                        "sound": true,
                        "alert": true
                    }
                }
            });
        } else {


            var uid = uuid2.newuuid();

            var details = {
                'email': uid + '@example.com',
                'password': 'secretpassword'
            };

            Ionic.Auth.signup(details).then(function () {
                var options = { 'remember': true };
                Ionic.Auth.login('basic', options, details).then(function () {
                    user = Ionic.User.current();
                    user.set('uid', uid);
                    user.save();

                    var push = newIonic.Push({
                        "debug": true,
                        "onNotification": function (notification) {                                
                        },
                        "onRegister": function (data) {
                            console.log(data.token);
                            returntrue;
                        },
                        "pluginConfig": {
                            "android": {
                                "icon": "icon"
                            },
                            "ios": {
                                "badge": true,
                                "sound": true,
                                "alert": true
                            }
                        }
                    });


                    push.register(function (token) {
                        console.log("Device token:", token.token);
                        push.saveToken(token);
                    });
                }, function () { });
            }, function () { });

        }

Solution 2:

In order to send push notification you need Api key and project number and current device id.

I think you are struggling in getting the user device id in order to get the current your device id please reffer ng-Cordova

you can find a line

$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) 

in this you can see the notification parameter it is an object inside that object you can find regid field in that you can get your current device id this will be work on only mobile not on browser.

so in order to use that device id, for example lets assume your going to post a login form with divece id like given below.

$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
          console.log(event);
          console.log(notification);

          switch(notification.event) {
            case'registered':
            console.log(notification.regid.length);
              if (notification.regid.length > 0 ) {
               // alert('registration ID = ' + notification.regid);console.log('registration ID = ' + notification.regid);
                 var loginPost = {
                    "UserName":username,
                    "PassWord":password,
                    "DeviceID":notification.regid
                  };
                  console.log(loginPost);

Post a Comment for "Ionic Push Notifications On Android Doesn't Register Token On Ionic.io"