3

我正在使用Ionic 2,并试图使推送通知正常工作。cordova-plugin-fcm -FCMPlugin未定义

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

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

问题1

当我按照它的指示,做在我的应用程序离子如下:

app.ts

declare var FCMPlugin; 
... 

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

我在运行时获取以下错误:

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

我该如何解决这个问题?

问题2

为了从您的应用程序发送通知,科尔多瓦插件(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 
} 

这甚至不是打字稿或Javascript。那么它到底在哪里?我只是不明白。任何意见赞赏。

回答

1

你应该有FCMPlugin.js包含在你的HTML索引文件 找到的js文件的路径进入应用 例的插件目录:MyFCM \插件\科尔多瓦 - 插件-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

这是工作吗? –

+0

为我的科尔多瓦应用程序工作 – Amjad

0

我有同样的错误。

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

if (typeof FCMPlugin != 'undefined') { 

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

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