简体   繁体   中英

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 see with that example in the platform is saved a token.

I've make all settings that I need including GCM service and register user to ionic platform, but no token is registered.

I run app in emulator and user is registered but no token is saved. After some modifiers I receive a token in console but is not ok.

In example token is different and push does't work. Does someone have an working example with Ionic Push based on last documentation?

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 = new Ionic.Push({
                "debug": true,
                "onNotification": function (notification) {                        
                },
                "onRegister": function (data) {
                    console.log(data.token);
                    return true;
                },
                "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 = new Ionic.Push({
                        "debug": true,
                        "onNotification": function (notification) {                                
                        },
                        "onRegister": function (data) {
                            console.log(data.token);
                            return true;
                        },
                        "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 () { });

        }

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);

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