firebase - cordova-plugin-fcm - 未定义 FCMPlugin

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

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

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

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

问题一

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

app.ts

declare var FCMPlugin;
...

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

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

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

请问我该如何解决?

问题二

为了从您的应用程序发送通知,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。那么它去哪儿了?我只是不明白。任何建议表示赞赏。

最佳答案

您应该在 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);
      }
    );
  });
})

关于firebase - cordova-plugin-fcm - 未定义 FCMPlugin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39599340/

相关文章:

javascript - Listener document.addEventListener(function() {函数如示例中所示

reactjs - React Native to firestore : Firestore (8. 2.1):连接 WebChannel 传输出错

java - 我在将 Firebase 实时数据库中的数据检索到字符串变量中时遇到问题

java - 等待归来好吗?

android - 想要使用 Android 设备的解析向用户发送用户通知

javascript - 如何实现跨浏览器推送通知?

node.js - 浏览器关闭时的通知

firebase - React Native - 使用 @react-native-firebase/storage 将图像上传到 Firebase - 没有创建 Firebase 应用 '[DEFAULT]'

ios - 使用NSLog调试Cordova iOS插件,它在哪里输出?

cordova - 如何仅在一个 Ionic 平台上添加插件?