一段时间以来,我一直在试图弄清楚如何发送推送通知。我所做的应用程序目前是Android的,但我想把它扩展到其他设备,一旦我弄清楚了。我研究过各种服务,比如Amazon SNS,但它们都忽略了如何获得设备令牌。他们都认为你知道怎么做。
所以我要问的是:我如何获得设备令牌/注册ID的设备?
我试过使用这个代码:
var tokenID = "";
document.addEventListener("deviceready", function(){
//Unregister the previous token because it might have become invalid. Unregister everytime app is started.
window.plugins.pushNotification.unregister(successHandler, errorHandler);
if(intel.xdk.device.platform == "Android")
{
//register the user and get token
window.plugins.pushNotification.register(
successHandler,
errorHandler,
{
//senderID is the project ID
"senderID":"",
//callback function that is executed when phone recieves a notification for this app
"ecb":"onNotification"
});
}
else if(intel.xdk.device.platform == "iOS")
{
//register the user and get token
window.plugins.pushNotification.register(
tokenHandler,
errorHandler,
{
//allow application to change badge number
"badge":"true",
//allow application to play notification sound
"sound":"true",
//register callback
"alert":"true",
//callback function name
"ecb":"onNotificationAPN"
});
}
}, false);
//app given permission to receive and display push messages in Android.
function successHandler (result) {
alert('result = ' + result);
}
//app denied permission to receive and display push messages in Android.
function errorHandler (error) {
alert('error = ' + error);
}
//App given permission to receive and display push messages in iOS
function tokenHandler (result) {
// Your iOS push server needs to know the token before it can push to this device
// here is where you might want to send the token to your server along with user credentials.
alert('device token = ' + result);
tokenID = result;
}
//fired when token is generated, message is received or an error occured.
function onNotification(e)
{
switch( e.event )
{
//app is registered to receive notification
case 'registered':
if(e.regid.length > 0)
{
// Your Android push server needs to know the token before it can push to this device
// here is where you might want to send the token to your server along with user credentials.
alert('registration id = '+e.regid);
tokenID = e.regid;
}
break;
case 'message':
//Do something with the push message. This function is fired when push message is received or if user clicks on the tile.
alert('message = '+e.message+' msgcnt = '+e.msgcnt);
break;
case 'error':
alert('GCM error = '+e.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
}
//callback fired when notification received in iOS
function onNotificationAPN (event)
{
if ( event.alert )
{
//do something with the push message. This function is fired when push message is received or if user clicks on the tile.
alert(event.alert);
}
if ( event.sound )
{
//play notification sound. Ignore when app is in foreground.
var snd = new Media(event.sound);
snd.play();
}
if ( event.badge )
{
//change app icon badge number. If app is in foreground ignore it.
window.plugins.pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
}
}
我得到的只是一个提示"result = ok"。代码后面的警报不会发生。我试着弄明白这些代码,但我一无所获。有什么建议吗?有没有我找不到的教程?
那些遗留的情报。XDK函数正在退役(将继续驻留在01.org中,请参阅本页的通知:https://software.intel.com/en-us/node/492826)。
我建议你从众多可用的推送通知Cordova插件中选择一个进行调查。使用你最喜欢的网络搜索工具,搜索一些类似"cordova phonegap推送通知插件"的东西来找到一些。
说明:-
- Unregister -不需要严格调用.....
- 确保你有Android的发件人ID(不知道iOS)。
Result OK
表示插件安装正确且运行正常
问题可能是由于:
- 发件人ID不正确
- 在没有适当设置的模拟器中进行测试
重要 -推送通知用于真实设备。它们没有针对WP8模拟器进行测试。注册过程将在iOS模拟器上失败。通知可以在Android模拟器上工作,但是这样做需要安装一些辅助库,如这里所述,在标题为"安装辅助库和设置模拟器"的一节中。
-
onNotification
必须作为全局对象可用。所以试着把它连接到window
上。参考这个问题
正确初始化PushPlugin的示例:
- Ionic(我的回答)