嗨,我按照以下步骤
1)我创建了cordova项目结构 .
2)我添加了平台(android) .
3)我添加了cordova插件
cordova plugin add https://github.com/phonegap-build/PushPlugin.git#2.4.0
4)建造cordova项目 .
5)接下来我在android eclipse中导入创建的应用程序(4.4.2)
6)我在 index.js
文件中写了下面的代码
init: function(){
alert("init");
var pushNotification = window.plugins.pushNotification;
pushNotification.register(successHandler, errorHandler,
{
'senderID':'XXXXXXXXXX',
'ecb':'onNotificationGCM' // callback function
}
);
function successHandler(result) {
console.log('Success: '+ result);
alert(result);
}
function errorHandler(error) {
console.log('Error: '+ error);
}
function onNotificationGCM(e) {
alert("comming");
if('registered' === e.event) {
// Successfully registered device.
}
else if('error' === e.event) {
// Failed to register device.
}
};
我得到的肯定是“OK” . 我无法调用'ecb':onNotificationGCM'//回调函数
在Android控制台中,我收到了消息
V/PushPlugin(2512): execute: action=register
V/PushPlugin(2512): execute: data= [{"senderID":"889953963751","ecb":"onNotificationGCM"}] V/PushPlugin(2512): execute: jo={"senderID":"889953963751","ecb":"onNotificationGCM"} V/PushPlugin(2512): execute: ECB=onNotificationGCM senderID=889953963751
09-12 03:13:33.453: D/GCMRegistrar(2512): resetting backoff for com.ensis.hello
09-12 03:13:33.613: V/GCMRegistrar(2512): Registering app com.ensis.hello of senders 889953963751
09-12 W/PluginManager(2512): THREAD WARNING: exec() call to PushPlugin.register blocked the main thread for 181ms. Plugin should use CordovaInterface.getThreadPool().
1 回答
这是推送通知流程:
您的应用请求注册远程Apple或Google服务器
如果注册正常,则删除服务器会返回一个令牌,用于标识设备上的此特定应用
您将此令牌发送到您的服务器,保存它(例如在数据库上)
您发送推送通知(从您的服务器)调用Apple或Google服务,并附上您要通知的用户的消息和令牌
这些服务会向您的设备/应用程序发送包含该消息的通知
您必须执行所有这些步骤才能使推送通知正常工作 .
对于android,你需要在通知处理程序的
registered
事件中捕获注册ID(令牌):对于iOS,您需要在
register
函数的succesHandler中捕获它 .有关更多信息,请查看this example in the plugin repository .