Generate FCM Token in ionic 1


#1

How to generate FCM token in ionic 1??I don’t know nothing about it


#2

for getting firebase token do this

  if (window.FirebasePlugin) {
           var Token  
            // window.FirebasePlugin.grantPermission(); //in ios
            window.FirebasePlugin.getToken(function (token) {
               
                Token = token;
                console.log(token);
            }, function (error) {
               
            });

            window.FirebasePlugin.onTokenRefresh(function (token) {
               
                Token = token;
                console.log(token);
                
               
            }, function (error) {
                
            });
        }

hopping that
1)Your created android project in firebase console ( and downloaded google-services.json file)
2) using firebase pluggin etc


#3

New Project will be create in firebase console or existing???
and tell me firebase plugin
and whare I use this code
and project which will be create in firebase console it is only android???If yes then what about iOS platform


#4

“cordova-plugin-firebase” this is the plugin that you need to install .

https://firebase.google.com/docs/android/setup

in firebase you need to create separate apps for both ios and android

you can use above code in a controller or a service .
if you want to send notifications to a specific user then you need to keep this firebase token in backend or api
if you are going to send broadcast messages then no need to keep this tokens


#5

Hey I followed steps what you suggested me…Thanks for it but when I keep your code in my code that time it is not working…check my code it is right or wrong

$ionicPlatform.ready(function () {
$scope.$apply(function () {

        if (window.FirebasePlugin) {
            var Token
            window.FirebasePlugin.grantPermission(); //in ios
            window.FirebasePlugin.getToken(function (token) {

                Token = token;
                console.log(token);
            }, function (error) {

            });

            window.FirebasePlugin.onTokenRefresh(function (token) {

                Token = token;
                console.log(token);


            }, function (error) {

            });
            alert(token);
        }
        
    }

#6

Let me know what was the error?


#7

No error is occurring but I can’t alert my FCM


#8

there is no log message also right? one more thing are you checking in ripple? this code not works with ripple check with a device


#9

ohkk…thank you for help…but I solved it by this :point_down:


#10

in which file i should add the gettoken(), onrefresh() ?


#11

for ionic 1 on file app.js