Home > OS >  How to get FCM token in Cordova?
How to get FCM token in Cordova?

Time:11-06

I try to implement push notification in android mobile app. Using

cordova-plugin-firebase-messaging

I can send push notification to android mobile using firebase console , but what happen those are installed apk file , all are receive push notifications, but i want to send to specific users, so i want to get FCM token.

What i am tried, in 'index.js' file, onDeviceReady function i call the below function to get token.

document.addEventListener('deviceready', onDeviceReady, false);

function onDeviceReady() {

    FCMPlugin.onTokenRefresh(function(token){
        alert( token );
    });

    FCMPlugin.getToken(function(token){
        alert(token);
    });

}

When i build and install apk file and open in mobile device, but nothing show when open device. My Cordova version is 11.0.0 and installed plugin is

cordova-plugin-fcm-with-dependecy-updated 7.8.0 "Cordova FCM Push Plugin"

Any reply much appreciate.

Regards Aravind

CodePudding user response:

You need to send it to Firebase using your back-end, by sending your notification to the Firebase endpoint https://fcm.googleapis.com/fcm/send

CodePudding user response:

You need to add the following script to initialize the firebase settings

<script type="module">
        // Import the functions you need from the SDKs you need
        import { initializeApp } from "https://www.gstatic.com/firebasejs/9.10.0/firebase-app.js";
        import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.10.0/firebase-analytics.js";
        // TODO: Add SDKs for Firebase products that you want to use
        // https://firebase.google.com/docs/web/setup#available-libraries
      
        // Your web app's Firebase configuration
        // For Firebase JS SDK v7.20.0 and later, measurementId is optional
        const firebaseConfig = {
          apiKey: "YOUR_API_KEY",
          authDomain: "YOUR_AUTH_DOMAIN (get it from your firebase project)",
          databaseURL: "YOUR_FIREBASE_DB_PATH",
          projectId: "YOUR_PROJECT_ID",
          storageBucket: "YOUR_STORAGE_BUCKET",
          messagingSenderId: "YOUR_SENDERS_ID",
          appId: "YOUR_APP_ID",
          measurementId: "G-4TN45ZQFFJ"
        };
      
        // Initialize Firebase
        const app = initializeApp(firebaseConfig);
        const analytics = getAnalytics(app);
      </script>

make sure you have done the following

  1. Created a firebase project and taken the above code directly from your project settings window (firebase has given a provision where it displays that code above along with your credentials, simple copy that and paste on your index.html page)

  2. Your google-services.json, which you have to download from your firebase project needs to be in the root directory.

  3. Once all that is done, you need to use the below code:

    cordova.plugins.firebase.messaging.getToken().then(function(token) { //save the token in a table on your server. Send the payload to that token.

    }

  4. once you have the tokens saved and you send a notification, you will need to catch the notification as under.

    cordova.plugins.firebase.messaging.onBackgroundMessage(function(payload) { //do something with the payload received/notification received

    }

  • Related