cordova-plugin-fcm - FCMPlugin is not defined

2019-02-18 14:37发布

问题:

I am using Ionic 2, and am trying to get Push Notifications working.

I have registered my app with Firebase, and can push notifications to it successfully.

I now need to set up, so that I can push notifications from my app. So I decided to use the following Cordova Plugin (cordova-plugin-fcm).

Question 1

When I follow it's instructions, by doing the following in my Ionic app:

app.ts

declare var FCMPlugin;
...

  initializeApp() {
    this.platform.ready().then(() => {
...
    FCMPlugin.getToken(
      function (token) {
....

I get the following Error at runtime:

EXCEPTION: Error: Uncaught (in promise): ReferenceError: FCMPlugin is not defined

How do I solve this please?

Question 2

In order to send notifications from your app, the Cordova Plugin (cordova-plugin-fcm) instructs the following:

//POST: https://fcm.googleapis.com/fcm/send 
//HEADER: Content-Type: application/json 
//HEADER: Authorization: key=AIzaSy******************* 
{
  "notification":{
    "title":"Notification title",  //Any value 
    "body":"Notification body",  //Any value 
    "sound":"default", //If you want notification sound 
    "click_action":"FCM_PLUGIN_ACTIVITY",  //Must be present for Android 
    "icon":"fcm_push_icon"  //White icon Android resource 
  },
  "data":{
    "param1":"value1",  //Any data to be retrieved in the notification callback 
    "param2":"value2"
  },
    "to":"/topics/topicExample", //Topic or single device 
    "priority":"high", //If not set, notification won't be delivered on completely closed iOS app 
    "restricted_package_name":"" //Optional. Set for application filtering 
}

This is not even Typescript or Javascript. So where does it go? I just don't understand. Any advise appreciated.

回答1:

You should have FCMPlugin.js included in your HTML index file find the path for js file into plugins directory of the app Example : MyFCM\plugins\cordova-plugin-fcm\www\FCMPlugin.js

app.controller('AppCtrl', function(FCMPlugin,$scope,$cordovaToast,$cordovaDialogs,ionPlatform) {
  // call to register automatically upon device ready
  ionPlatform.ready.then(function (device) {
    console.log('I am working');
    FCMPlugin.onNotification(
      function(data){
        if(data.wasTapped){
          //Notification was received on device tray and tapped by the user.
          $cordovaDialogs.alert(data.notification.body);
        }else{
          //Notification was received in foreground. Maybe the user needs to be notified.
          $cordovaDialogs.alert(data.notification.body);
          //$cordovaToast.showShortCenter( JSON.stringify(data) );
        }
      },
      function(msg){
        $cordovaToast.showShortCenter('onNotification callback successfully registered: ' + msg);
      },
      function(err){
        $cordovaToast.showShortCenter('Error registering onNotification callback: ' + err);
      }
    );
  });
})


回答2:

i had same eror.

try this. it will definitely help you.

  if (typeof FCMPlugin != 'undefined') {

      FCMPlugin.getToken(function (token) {
          console.log(token);
      });
  }

i put above "if condition" and it solves my issue.