Ionic push not working - user not registered

2019-02-15 12:31发布

I just had my first contacts with the ionic framework. I've worked with Phonegap and AngularJS before however, so it was not all that new to me. I found out that there is this new feature to use Google Cloud Messaging push notifications in Ionic, through the Ionic Push feature (http://blog.ionic.io/announcing-ionic-push-alpha/).

Related lines of code from app.js

angular.module('starter', ['ionic','ionic.service.core', 'starter.controllers', 'starter.services'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
  // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
  // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleLightContent();
    }

    // enable push notifications
    Ionic.io(); 

    // enable users (http://docs.ionic.io/docs/user-quick-start)
    // this will give you a fresh user or the previously saved 'current user'
    var user = Ionic.User.current();

    // if the user doesn't have an id, you'll need to give it one.
    if (!user.id) {
      user.id = Ionic.User.anonymousId();
      // user.id = 'your-custom-user-id';
    }
    console.log('user-id:' + user.id);

    //persist the user
    user.save();

    var push = new Ionic.Push({
      "debug": true,
      "onNotification": function(notification) {
        var payload = notification.payload;
        console.log(notification, payload);
      },
      "onRegister": function(data) {
        console.log(data.token);
      }
    });

    push.register(function(token) {
      console.log("Device token:",token.token);
    });
    push.addTokenToUser(user);
    console.log('token added to user');

  });
})

Log from ionic serve

ionic $ 0     361081   log      Ionic Core:, init
1     361083   log      Ionic Core:, searching for cordova.js
2     361085   log      Ionic Core:, attempting to mock plugins
3     361155   log      user-id:1cc3d21c-b687-4988-b944-ad07b1a677c8
4     361158   log      Ionic Push:, a token must be registered before you can add it to a user.
5     361159   log      Ionic Push:, token is not a valid Android or iOS registration id. Cannot save to user.
6     361159   log      token added to user
7     361160   log      Ionic Push:, register
8     361160   error    ReferenceError: PushNotification is not defined, http://localhost:8100/lib/ionic-platform-web-client/dist/ionic.io.bundle.min.js, Line: 2
9     361526   log      Ionic User:, saved user

Any input is welcome, I am also more than happy to provide more information if needed.

EDIT 10/05/2015:

  • found out that dev_push = false only works on physical devices, not in browser
  • I tried to add token to user before even registering the user

5条回答
Fickle 薄情
2楼-- · 2019-02-15 13:17

I have it like this and it works:

        Ionic.io();

        var user = Ionic.User.current();

        if (!user.id) {
            user.id = Ionic.User.anonymousId();

            // save our newly created user
            user.save();
        }

        var push = new Ionic.Push({});

        push.register(function (token) {
            console.log("Got Token:", token.token);

            // now we have token, so add it to user
            push.addTokenToUser(user);

            // don't forget to save user to Ionic Platform with our new token
            user.save();
        });

        // set this user as current, so we can acess him later
        Ionic.User.current(user);
查看更多
一夜七次
3楼-- · 2019-02-15 13:25

Did you use this

ionic config set dev_push true-if testing in emulator or laptop
ionic config set dev_pushfalse - if testing on the phone

ionic push --google-api-key Your API Key

ionic config set gcm_key Project Number

Your token is the registration id that is unique for a particular device. That is sent to you by android.

Your Phone (With the API Key)---------> to Google's GCM
Google GCM (recognises it's you via your Project number and API key) -----> Oh it's you let me make a note of it. (Save a token id in it's DB and send's one to you.)

You get a registration id unique for your device(will change if an app is uninstalled). You call your server say hey it's me your user. Please Notify me if you get something.

Server obliges, says, okay dude, I got this. Saves the registration id with your details probably your username in it's DB.


Now from Server.

I need to inform my users about a great deal(or an accident or something).
Pull up all targeted registration Id's from DB(maybe on some condition)

registrationIds.push(regId) //in a loop

and sender.send(message, registration, function(err, result){
});

Send to Google. Google see's oh only these many people(not all maybe) from this API Key need a notification. no Problem I will notify them and you receive a notification, my dear friend.

查看更多
放我归山
4楼-- · 2019-02-15 13:26

As mentioned in the link , Adding token to the $ionicUser is done by doing , user.addPushToken(pushToken); .

For this to work you should first configure the app not to use developement pushes by ,

ionic config set dev_push true

After initialising Ionic.io and Ionic.push , load the user or create one with a new random id ,

  Ionic.io();

  var push = new Ionic.Push();

  Ionic.User.load(localStorage.id).then(function (user) {
    Ionic.User.current(user);
    pushFactory.register(push, user);
  }, function (error) {
    /*the user with that id is not present , so create one with a random id and register the push */
  });

The push factory is defined as below,

function pushFactory() {

return {
  'register': function (push, user) {
    push.register(function (pushToken) {
      user.addPushToken(pushToken);
      user.save().then(function (answer) {
        console.log('user saved'+answer);
      })
    })
  }
}
}
查看更多
Bombasti
5楼-- · 2019-02-15 13:27

You also need to declare 'ionic.service.push' as a dependency in your angular module if you'd like to use it.

angular.module('starter', ['ionic','ionic.service.core', 'ionic.service.push'])

查看更多
淡お忘
6楼-- · 2019-02-15 13:28

I'm having the same problem, seems not many answers online at the moment.

but even on real device, it won't save the token to user.

I just had to decide go live without push first, then use ionic deploy to follow up.

also I think you have to put this line push.addTokenToUser(user);

inside the register callback according to this doc http://docs.ionic.io/docs/push-usage

查看更多
登录 后发表回答