在Phonegap中推送通知?

时间:2016-12-15 13:41:32

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

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

1 个答案:

答案 0 :(得分:0)

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);
    });
},
};
相关问题