【问题标题】:cordova-plugin-fcm - FCMPlugin is not definedcordova-plugin-fcm - 未定义 FCMPlugin
【发布时间】:2016-09-20 16:27:48
【问题描述】:

我正在使用 Ionic 2,并且正在尝试让推送通知正常工作。

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

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

问题 1

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

app.ts

declare var FCMPlugin;
...

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

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

异常:错误:未捕获(承诺中):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。那么它要去哪里呢?我只是不明白。任何建议表示赞赏。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 为我的科尔多瓦应用工作
    • 它可以在 chrome 浏览器中进行测试吗?还是只在手机上?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 2018-08-04
    相关资源
    最近更新 更多