cordova-plugin-fcm - 未定义FCMPlugin

时间:2016-09-20 16:27:48

标签: firebase push-notification cordova-plugins ionic2 firebase-cloud-messaging

我正在使用Ionic 2,并且我正在努力让Push Notifications正常工作。

我已经使用Firebase注册了我的应用,并且可以成功向其推送通知。

我现在需要设置,以便我可以从我的应用推送通知。所以我决定使用以下Cordova插件(cordova-plugin-fcm)。

问题1

当我按照说明操作时,请在我的Ionic应用程序中执行以下操作:

app.ts

declare var FCMPlugin;
...

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

我在运行时遇到以下错误:

  

EXCEPTION:错误:未捕获(在承诺中):ReferenceError:FCMPlugin是   未定义

我该如何解决这个问题?

问题2

为了从您的应用发送通知,Cordova插件(cordova-plugin-fcm)会指示以下内容:

//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 
}

这甚至不是Typescript或Javascript。那它去哪儿了?我只是不明白。任何建议表示赞赏。

2 个答案:

答案 0 :(得分:1)

您的HTML索引文件中应包含FCMPlugin.js 找到js文件的路径到app的plugins目录 示例: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);
      }
    );
  });
})

答案 1 :(得分:-1)

我有同样的错误。

试试这个。它肯定会帮助你。

  if (typeof FCMPlugin != 'undefined') {

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

我把上面的“如果条件”并且它解决了我的问题。