我在移动设备中使用PubNub进行推送通知.我为我的项目使用了以下代码.按照以下链接的方向. https://www.pubnub.com/blog/2014-12-18-sending-android-push-notifications-via-gcm-javascript-using-phonegap/ var pushNo
https://www.pubnub.com/blog/2014-12-18-sending-android-push-notifications-via-gcm-javascript-using-phonegap/
var pushNotification = window.plugins.pushNotification;
pushNotification.register(
successHandler,
errorHandler,
{
'senderID':'projectID'
}
);
function successHandler(result) {
alert('Success: '+ result);
}
function errorHandler(error) {
alert('Error: '+ error);
}
但是这个推送通知寄存器调用errorHandler函数.它显示错误消息“Class Not Found”.
为什么我收到此错误.?
任何人请建议
新的更新
根据上面的链接,我在我的cordova项目中添加了以下代码.
function initialize() {
bindEvents();
}
function bindEvents() {
document.addEventListener('deviceready', init, false);
}
function init() {
var pushNotification = window.plugins.pushNotification;
pushNotification.register(successHandler, errorHandler, {'senderID':'projectID','ecb':'onNotificationGCM'});
// Get your Sender ID at cloud.google.com/console
}
function successHandler(result) {
alert('Success: '+ result);
}
function errorHandler(error) {
alert('Error: '+ error);
}
function onNotificationGCM(e) {
switch( e.event ){
case 'registered':
if ( e.regid.length > 0 ){
console.log('regid = '+e.regid);
alert(e.regid);
}
break;
case 'message':
console.log(e);
if (e.foreground){
alert('The room temperature is set too high')
}
break;
case 'error':
alert('Error: '+e.msg);
break;
default:
console.log('An unknown event was received');
break;
}
}
initialize();
我在模拟器上运行此代码.它只是成功调用successHandler函数“OK”.它没有调用onNotificationGCM上的回调函数,我没有得到注册的ID.
我的cordova项目代码背后的实际问题是什么?
请建议我
您需要一个ecb事件回调函数,例如:pushNotification.register(
successHandler,
errorHandler,
{
'senderID':'your_sender_id',
'ecb':'onNotificationGCM' // callback function
}
);
回调函数应如下所示:
function onNotificationGCM(e) {
case 'registered':
if (e.regid.length > 0){
deviceRegistered(e.regid);
}
break;
case 'message':
if (e.foreground){
// When the app is running foreground.
}
break;
case 'error':
console.log('Error: ' + e.msg);
break;
default:
console.log('An unknown event was received');
break;
}
另外,你可以看一下插件的GitHub repo上的readme.md.请注意,该插件不再维护并标记为“已弃用”.
