cordova-plugin-fcm - 未定义 FCMPlugin [英] cordova-plugin-fcm - FCMPlugin is not defined

查看:10
本文介绍了cordova-plugin-fcm - 未定义 FCMPlugin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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).

问题 1

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

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:

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

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

请问我该如何解决?

问题 2

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

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 
}

这甚至不是 Typescript 或 Javascript.那么它要去哪里呢?我只是不明白.任何建议表示赞赏.

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

推荐答案

您的 HTML 索引文件中应该包含 FCMPlugin.js在应用程序的插件目录中找到 js 文件的路径示例:MyFCMpluginscordova-plugin-fcmwwwFCMPlugin.js

You should have FCMPlugin.js included in your HTML index file find the path for js file into plugins directory of the app Example : MyFCMpluginscordova-plugin-fcmwwwFCMPlugin.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);
      }
    );
  });
})

这篇关于cordova-plugin-fcm - 未定义 FCMPlugin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆