Device token is blank when trying push notification by Cordova plugin for Firebase Cloud Messaging

606 Views Asked by At

I am tring to do push notification on my cordova android app with Cordova plugin for Firebase Cloud Messaging plugin. I have created and registered app in Google firebase console and downloaded google-services.json file and kept it in my project root folder. Then I am trying the functions provided in the plugin documentation on device ready but did not get the device token, it is blank. My cordova android version is 9.1.0. Can someone tell me why the device token is not generated? Please help me. My js code is below:

    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        cordova.plugins.firebase.messaging.onMessage(function (payload) {
            console.log("New foreground FCM message: ", payload);
        });

        cordova.plugins.firebase.messaging.onBackgroundMessage(function (payload) {
            console.log("New background FCM message: ", payload);
        });

        cordova.plugins.firebase.messaging.requestPermission({ forceShow: true }).then(function () 
       {
            console.log("You'll get foreground notifications when a push message arrives");
       });

        cordova.plugins.firebase.messaging.getToken().then(function (token) {
            alert("Got device token: ", token);
        });

        /*cordova.plugins.firebase.messaging.onTokenRefresh(function () {
            console.log("Device token updated");
        });*/

        cordova.plugins.firebase.messaging.createChannel({
            id: "mypushid",
            name: "Push channel",
            importance: 3
        });
    }

0

There are 0 best solutions below