【问题标题】:Push Notification in Phonegap?在Phonegap中推送通知?
【发布时间】:2016-12-15 13:41:32
【问题描述】:

我正在使用 Phonegap 创建一个移动应用程序。我想使用 FCM 创建一个推送通知。我使用了 cordova-plugin-firebase 插件。但它显示错误。确实卡在项目中

【问题讨论】:

  • 到目前为止你做了什么??请在此处发布您的代码。
  • 我安装了以下插件
  • 在这里发布您的错误,您遇到了什么错误?
  • 我把上面的插件和下面的代码放在我的设备就绪窗口中。FirebasePlugin.getToken(function(token) { // 保存这个服务器端并用它来推送通知到这个设备控制台.log(token); }, function(error) { console.error(error); });
  • 它显示 Uncaught SyntaxError: Unexpected token :

标签: cordova push-notification phonegap-plugins cordova-plugins


【解决方案1】:

1) 安装插件(你已经这样做了)

cordova plugin add cordova-plugin-firebase@0.1.18 --save

2) 从您的 firebase 帐户下载两个文件并将文件保存在您的根文件夹中:

google-services.json
GoogleService-Info.plist 

3) 在您的 onDeviceReady() 方法中编写此代码:

onDeviceReady: function() {  
  window.FirebasePlugin.onTokenRefresh(function(token) {
    //save this server-side and use it to push notifications to this device
     console.log(token);
  }, function(error) {
    console.error(error);
 });
}

这里是完整的 index.js 代码:

var app = { 
// Application Constructor
initialize: function() {
    this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
    window.FirebasePlugin.onTokenRefresh(function(token) {
        //save this server-side and use it to push notifications to this device
         console.log(token);
      }, function(error) {
        console.error(error);
    });
},
};

【讨论】:

  • index.html文件中是否需要添加外部文件链接?
  • 我没有添加 GoogleService-Info.plist 文件,我只想要 Android。所以 GoogleService-Info.plist 文件是强制性的吗?
  • 仅适用于 ios,
  • 插件安装成功
  • 我认为不是firebase的问题,我认为你的js文件有问题,请重新检查。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-17
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 2013-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多