我正在使用 cordova 插件添加 phonegap-plugin-push --variable SENDER_ID=XXXXXXXX 这个插件。并将以下代码添加到 javascript 文件中。
function setupPush() {
var push = PushNotification.init({
"android": {
"senderID": "XXXXXXXX"
},
"ios": {
"sound": true,
"alert": true,
"badge": true
},
"windows": {}
});
push.on('registration', function(data) {
console.log("registration event: " + data.registrationId);
var oldRegId = localStorage.getItem('registrationId');
if (oldRegId !== data.registrationId) {
// Save new registration ID
localStorage.setItem('registrationId', data.registrationId);
// Post registrationId to your app server as the value has changed
}
});
push.on('error', function(e) {
console.log("push error = " + e.message);
});
}
它给出错误“TypeError:无法调用未定义的方法‘init’”。
最佳答案
如果您在 onDeviceReady
之后还没有这样做,通常会发生这种情况。在此触发之前,您无法访问任何 native 设备 API(推送通知、相机等)。
function onDeviceReady() {
// Sets up your push notifications
setupPush();
}
// Fires when device is ready.
document.addEventListener("deviceready", onDeviceReady, false);
关于android - 在 Cordova 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43347547/