Sharif Rifat
Sharif Rifat

Reputation: 131

FireBase push notification using cordova

I am trying to implement Firbase push notification using cordova. I use the code for the latest fcm plug in specificaion from here : Cordova Push Plugin

I can get the register token. Then I tried to send notification from Firebase test notification module using that token. Each time I run the app in my device I am having the alert-

"Msg: onNotification callback successfully registered: OK"

it is inside the second function of FCMPlugin.onNotification event.

But the first function [where I want to get the notification] is not called. I don't find where I am making mistake. Here is my code inside onDeviceReady:

function onDeviceReady() {
    // Handle the Cordova pause and resume events
    document.addEventListener( 'pause', onPause.bind( this ), false );
    document.addEventListener( 'resume', onResume.bind( this ), false );

    // TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.
    var parentElement = document.getElementById('deviceready');
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');
    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    //=========================
    FCMPlugin.getToken(
      function (token) {
          alert("Token: " + token);
              cordova.plugins.email.open({
                  to: '[email protected]',
                  subject: 'Greetings',
                  body: token
              });
      },
      function (err) {
          alert("Error: " + 'error retrieving token: ' + err);
      }
    );

    FCMPlugin.onNotification(
      function (data) {
          alert("Notify: " + JSON.stringify(data));
          if (data.wasTapped) {
              //Notification was received on device tray and tapped by the user. 
              alert("Wrapped Notify: " + JSON.stringify(data));
          } else {
              //Notification was received in foreground. Maybe the user needs to be notified. 
              alert("Notify: " + JSON.stringify(data));
          }
      },
      function (msg) {
          alert("Msg: " + 'onNotification callback successfully registered: ' + msg.Notification);
      },
      function (err) {
          alert("Error: " + 'Error registering onNotification callback: ' + err);
      }
    );
};

Upvotes: 2

Views: 5174

Answers (2)

JanP
JanP

Reputation: 1581

Make sure you add "click_action":"FCM_PLUGIN_ACTIVITY" to the payload for the REST API. This must be present for Android. If this isn't available you will NOT receive data from a tapped notification (or hear a sound).

See the REST API payload example from the cordova-plugin-fcm documentation:

//POST: https://fcm.googleapis.com/fcm/send
//HEADER: Content-Type: application/json
//HEADER: Authorization: key=AIzaSy*******************
{
  "notification":{
    "title":"Notification title",
    "body":"Notification body", 
    "sound":"default", 
    "click_action":"FCM_PLUGIN_ACTIVITY",  //  <<<<<<< Must be present for Android
    "icon":"fcm_push_icon"
  },
  "data":{
    "param1":"value1", 
    "param2":"value2"
  },
    "to":"/topics/topicExample", 
    "priority":"high", 
    "restricted_package_name":"" 
}

Upvotes: 2

Meher BA
Meher BA

Reputation: 31

You are missing to subscribe to your topic before onNotification function like this :

FCMPlugin.subscribeToTopic('topic');

Upvotes: 1

Related Questions