我在此博客文章中关注howto:http : //www.pubnub.com/blog/sending-android-push-notifications-via-gcm-javascript-using-phonegap/
到目前为止,我已经完成了以下操作:
然后将推插件添加到现有项目:
$ cordova plugin add https://github.com/phonegap-build/PushPlugin.git
然后将PushNotification.js从插件复制到我的js lib文件夹中。我正在用requirejs加载此js文件和pubnub cdn,这似乎工作正常。
我使用howto中的2.2脚本创建了一个模块。
define(['env-config','datastore/localstore','jquery','push','pubnub'],function (EnvConfig,Store) {
var pushNotification = window.plugins.pushNotification;
function register() {
pushNotification.register(
successHandler,
errorHandler,
{ 'senderID':EnvConfig.push.senderid, 'ecb':'onNotificationGCM'}
);
}
function successHandler(result) {
console.log('Success: '+ result);
}
function errorHandler(error) {
//** The following line throws:
//** java.lang.Long cannot be cast to java.lang.String
console.log('Error:' + error);
}
function onNotificationGCM(e) {
switch(e.event){
case 'registered':
console.log("Device registered with id "+e.regid);
Store.set("pushid"+e.regid);
/*if (e.regid.length > 0) {
deviceRegistered(e.regid);
} */
break;
case 'message':
if (e.foreground){
//What needs to be done when app is in the foreground
//while receiving a notification
console.log('A notification has been received');
}
break;
case 'error':
console.log('Error: ' + e.msg);
break;
default: console.log('An unknown event was received');
break;
}
}
var module = {
init: function() {
register();
}
};
return module;
});
设备准备就绪后,我将调用此模块的init函数。
此刻register()失败并执行errorHandler回调。但是console.log失败,并出现:无法将java.lang.Long强制转换为java.lang.String。因此我不知道下一步该怎么做...
非常感谢任何指针。
三星Galaxy S5
Android 4.4.2
Chrome 30.0(在网络
视图中)科尔多瓦3.6.3-0.2.13
嗯,我想我知道这是怎么回事-您的发件人ID必须是字符串!您可能将其用作数字。
顺便说一句,感谢您尝试我写的教程:-)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句