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);
});
},
};