Software Archive
Read-only legacy content
17061 Discussions

Best practice to get FCM token on app first run

madson_g_
Beginner
361 Views

I implemented cordova phonegap plugin push adding the plugin in Plugin Management tool with npm option. It works fine except for one detail:

When I download the app from build.phonegap.com and click to install it, it installs as expected. So buttons are displayed to open the app or close the installation box. If I click to open the app, I cannot get FCM token. 

    push.on('registration', function(data){ ... })

is not being executed and "token" is null.

So I have to close it and open it again clicking on app icon and everything works fine (I get the token while splashscreen is running and the app moves to the register user page).

Any suggestion to make it works on app first run?

.run(function($ionicPlatform, $state, $ionicPopup) {
  $ionicPlatform.ready(function() {
          
        var push = PushNotification.init({
            android: {
                senderID: "*************",
                sound: true,
                vibrate: true
            },
            browser: {},
            ios: {
                alert: "true",
                badge: "true",
                sound: "true"
            },
            windows: {}
        });

        push.on('registration', function(data) {
            //data.registrationId;
            localStorage.setItem("token", data.registrationId);
            console.log("token: "+data.registrationId);
        }); 

        push.on('notification', function(data) {
            // data.message,
            // data.title,
            // data.count,
            // data.sound,
            // data.image,
            // data.additionalData         
            
        });

        push.on('error', function(e) {
            // e.message
            //alert("Erro: " +e.message);
        });

        navigator.splashscreen.hide(); 
...
0 Kudos
2 Replies
PaulF_IntelCorp
Employee
361 Views

Sorry, but I'm not familiar with how the Ionic framework works. This question is better presented to Ionic, since the XDK and PhoneGap Build create a standard Cordova app, they should be able to answer it. I suspect it has something to do with initialization of the app relative to initialization of the Cordova/Ionic framework and the underlying Android app. But the best people to answer those questions would be Ionic.

If you say something like "I'm using the Intel XDK to create my app" they'll probably assume we are adding something that is affecting the behavior. In fact, the XDK is not adding anything to the Ionic framework or Cordova, it is strictly being used as a convenient edit and simulation tool. So I would not ask your question in that way, otherwise you might get a deflection.

However, I would attempt to build it locally, first, using Cordova CLI, to determine if there is a difference when built using CLI locally.

0 Kudos
madson_g_
Beginner
361 Views

I understand. The same issue is happening with an Ionic CLI project not using XDK.

I made some tests in xdk project clicking to get "token" from localstorage and it looks like localstorage is not returning "token" to my var token = localStorage.getItem("token") on signup page loading.in a first moment.

I created a button to get it and it´s working by the way. I don´t know what´s happening with localstorage.

0 Kudos
Reply