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

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

问题描述

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

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

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

问题1

当我按照指示操​​作时,我的离子应用程序:

app.ts

 声明var FCMPlugin; 
...

initializeApp(){
this.platform.ready()。then(()=> {
...
FCMPlugin.getToken(
函数(令牌){
...

在运行时出现以下错误:
$ b


EXCEPTION:错误:Uncaught(在promise中):ReferenceError:FCMPlugin是
未定义

如何解决此问题?

问题2为了从您的应用程序发送通知,Cordova插件( -plugin-fcmrel =nofollow> cordova-plugin-fcm )指示如下:

  / / POST:https://fcm.googleapis.com/fcm/send 
//头文件:Content-Type:application / json
//头文件:授权:key = AIzaSy ****** *************
{
通知:{
title:通知标题,//任何值
body :通知主体,//任何值
sound:default,//如果您想要通知声音
click_action:FCM_PLUGIN_ACTIVITY,//必须为Android
icon:fcm_push_icon//白色图标Android资源

data:{
param1:value1,//在通知回调中要检索的任何数据
param2:value2

to:/ topics / topicExample,//主题或单个设备
priority:high,//如果未设置,则通知将不会在完全关闭的iOS应用
restricted_pa​​ckage_name://可选。设置为应用程序过滤
}

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

解决方案

你应该有你的HTML索引文件
包含FCMPlugin.js找到js的路径文件插入应用程序的插件目录
示例:MyFCM \plugins\cordova-plugin-fcm\www\FCMPlugin.js

  app.controller('AppCtrl',function(FCMPlugin,$ scope,$ cordovaToast,$ cordovaDialogs,ionPlatform){
//设备准备好后自动注册
ionPlatform.ready (函数(device)){
console.log('I am working');
FCMPlugin.onNotification(
function(data){
if(data.wasTapped) {
//在设备托盘上收到通知,并由用户点击通知
$ cordovaDialogs.alert(data.notification.body);
} else {
//通知为收到在前台。可能需要通知用户
$ cordovaDialogs.alert(data.notification.body);
/ /$cordovaToast.showShortCenter(JSON.stringify(data));


函数{
$ cordovaToast.showShortCenter('onNotification callback successfully registered:'+ msg);
},
函数(err){
$ cordovaToast.showShortCenter('注册onNotification回调错误:'+ err);
}
);
});
})


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

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

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

Question 1

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:

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

How do I solve this please?

Question 2

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 
}

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

解决方案

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

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

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