我正在构建一个使用推送通知的Cordova / Phonegap的iOS应用程序.
我正在使用PushPlugin在客户端应用程序中实现通知.
我已经设置了APNS方面的东西,当我的应用程序暂停或关闭时发送通知时,通知会正确显示.但是,当我在应用程序中时,该插件会抛出错误:
Can't find variable: onNotificationAPN
2014-03-02 23:12:58.746 EASP 2014[5792:60b] Notification received 2014-03-02 23:12:58.747 EASP 2014[5792:60b] Msg: {"alert":"testing...",foreground:"1"}
但是在应用程序中没有任何反应,我遇到onNotificationAPN错误.
我已经尝试了一切来调试这个,但我被卡住了.知道为什么会这样吗?
// SET UP PUSH NOTIFICATIONS var pushNotification; pushNotification = window.plugins.pushNotification; if ( device.platform == 'android' || device.platform == 'Android' ) { pushNotification.register( successHandler,errorHandler,{ "senderID":"<xxxxx>","ecb":"onNotificationGCM" } ); } else { pushNotification.register( tokenHandler,{ "badge":"false","sound":"false","alert":"true","ecb":"onNotificationAPN" } ); } // result contains any message sent from the plugin call function successHandler (result) { console.log('result = ' + result); navigator.notification.alert( result,onConfirm,'Title of app','dismiss' ); } // result contains any error description text returned from the plugin call function errorHandler (error) { console.log('error = ' + error); } function tokenHandler (result) { var uuid = device.uuid; var platform = device.platform; console.log(platform); if (platform == 'iOS'){ var os = 'ios'; } else { var os = 'android'; } hash = result+'<title of app>'; hash = md5(hash); var xmlHttp = null; xmlHttp = new XMLHttpRequest(); var url = 'https://<notification server>/?token='+result+'&id='+uuid+'&hash='+hash+'&os='+os; xmlHttp.open( "GET",url,false ); xmlHttp.send( null ); console.log(xmlHttp.responseText); return xmlHttp.responseText; } // iOS function onNotificationAPN (event) { console.log(event); if ( event.alert ) { navigator.notification.alert(event.alert); //alert(event.alert); } if ( event.sound ) { var snd = new Media(event.sound); snd.play(); } if ( event.badge ) { pushNotification.setApplicationIconBadgeNumber(successHandler,event.badge); } } function receivedEvent(id) { navigator.notification.alert( id,'<title of app>','dismiss' ); } function onConfirm(buttonIndex,id) { }
解决方法
经过一些挖掘后管理让它工作.
这是现在的代码:
// SET UP PUSH NOTIFICATIONS var addCallback = function addCallback(key,callback) { if (window.pushCallbacks === undefined) { window.pushCallbacks = {} } window.pushCallbacks[key] = callback; }; var pushNotification; pushNotification = window.plugins.pushNotification; if ( device.platform == 'android' || device.platform == 'Android' ) { pushNotification.register( successHandler,{ "badge":"true","sound":"true","ecb":"pushCallbacks.onNotificationAPN" } ); } // result contains any message sent from the plugin call function successHandler (result) { console.log('result = ' + result); navigator.notification.alert( result,'dismiss' ); } // result contains any error description text returned from the plugin call function errorHandler (error) { console.log('error = ' + error); } function tokenHandler (result) { var uuid = device.uuid; var platform = device.platform; console.log(platform); if (platform == 'iOS'){ var os = 'ios'; } else { var os = 'android'; } hash = result+'<title of app>'; hash = md5(hash); var xmlHttp = null; xmlHttp = new XMLHttpRequest(); var url = '<title of app>/?token='+result+'&id='+uuid+'&hash='+hash+'&os='+os; console.log('URL IS: '+url); xmlHttp.open( "GET",false ); xmlHttp.send( null ); console.log(xmlHttp.responseText); addCallback('onNotificationAPN',onNotificationAPN); return xmlHttp.responseText; } // iOS function onNotificationAPN (event) { if ( event.alert ) { navigator.notification.alert(event.alert); } if ( event.sound ) { var snd = new Media(event.sound); snd.play(); } if ( event.badge ) { pushNotification.setApplicationIconBadgeNumber(successHandler,id) { }
所以基本上添加的是
var addCallback = function addCallback(key,callback) { if (window.pushCallbacks === undefined) { window.pushCallbacks = {} } window.pushCallbacks[key] = callback; };
在开始时,和
"ecb":"pushCallbacks.onNotificationAPN"
现在工作.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。