push notification with cordova and firebase Cloud messaging:registrationID keep changing

981 views Asked by At

Using cordova plugin ktekosi-phonegap-plugin-push, I get the registrationID

var push = PushNotification.init({ "android": {"senderID": "my sender ID"}});

push.on('registration', function(data) {
    console.log(data.registrationId);
    document.getElementById("fcm_id").innerHTML = data.registrationId;
});

So I can use it by the server to send notification through FCM.

How can I get this registrationID fixed for all time? Is this an app specific ID or a user specific ID?

2

There are 2 answers

8
Reaz Murshed On BEST ANSWER

Push registration ID will be changed with time. This is an usual case. This is neither an instance of the installed application nor app-specific or user-specific id.

When an app comes online you need to fetch the push registration ID and then pass it to your backend server each time to keep your backend server updated with the push registration ID.

Your backend server will then generate a push notification and will send it to FCM with the specific push registration ID it has. FCM then manages receiving the push notification in your mobile application.

I've answered a question here telling how push notification system works. You might have a look.

Update

Looks like google has changed its new gcm library as the answer stated here - https://stackoverflow.com/a/16839326/3145960. You might take a look at here. So, as I've read, the push registration id is updated only when app receives an update or the android os gets updated. Its much simpler now.

0
Anshul Khare On

Try to use this plugin "cordova plugin add cordova-plugin-fcm". You can find detail here

To get Registration ID Use it like this

<script type="text/javascript">
          function onLoad() {
          document.addEventListener("deviceready", Fire, false);
                               }

            function Fire() {

                FCMPlugin.getToken(
                  function (token) {

                       alert(token)
                  },
                  function (err) {
                           alert("Error: " + 'error retrieving token: ' + err);
                  }
                );

            };


        </script>
 <body onload="onLoad();">