我尝试使用pushNotification注册我的设备,使用演示 . 它不起作用 . 打印“Cordova PushNotification插件演示”和“注册android”并查看警报“OK”(successHandler函数有效) . 问题是“ecb”不起作用 . 为什么?你能帮助我吗?
我与Cordova一起安装的插件有:PushPlugin,Device,Notification,InAppBrowser和Network Information .
我在我的LG L9和模拟器上试过这个应用程序 . 同样的问题 .
这是代码:
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery_1.5.2.min.js"></script>
<script type="text/javascript" src="PushNotification.js"></script>
<script type="text/javascript">
var pushNotification;
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
function onDeviceReady() {
pushNotification = window.plugins.pushNotification;
if (device.platform == 'android' || device.platform == 'Android') {
$("#app-status-ul").append("<li>registering android</li>");
window.plugins.pushNotification.register(successHandler, errorHandler, {
"senderID": "my_id",
"ecb": "onNotificationGCM"
}); // required!
} else {
$("#app-status-ul").append("<li>registering iOS</li>");
pushNotification.register(tokenHandler, errorHandler, {
"badge": "true",
"sound": "true",
"alert": "true",
"ecb": "onNotificationAPN"
}); // required!
}
}
// handle APNS notifications for iOS
function onNotificationAPN(e) {
if (e.alert) {
navigator.notification.alert(e.alert);
}
if (e.sound) {
var snd = new Media(e.sound);
snd.play();
}
if (e.badge) {
pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
}
}
// handle GCM notifications for Android
function onNotificationGCM(e) {
navigator.notification.alert(e.event);
switch (e.event) {
case 'registered':
if (e.regid.length > 0) {
navigator.notification.alert(e.regid);
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
$("#app-status-ul").append("<li>regID = " + e.regid +"</li>");
sessionStorage.setItem("deviceId",e.regid);
}
break;
case 'message':
// if this flag is set, this notification happened while we were in the foreground.
// you might want to play a sound to get the user's attention, throw up a dialog, etc.
if (e.foreground) {
navigator.notification.alert('--INLINE NOTIFICATION--');
// if the notification contains a soundname, play it.
var my_media = new Media("/android_asset/www/" + e.soundname);
my_media.play();
} else { // otherwise we were launched because the user touched a notification in the notification tray.
if (e.coldstart) navigator.notification.alert('--COLDSTART NOTIFICATION--');
else navigator.notification.alert('--BACKGROUND NOTIFICATION--');
}
navigator.notification.alert(e.payload.message);
navigator.notification.alert('MESSAGE -> MSGCNT: ' + e.payload.msgcnt);
break;
case 'error':
navigator.notification.alert('ERROR -> MSG:' + e.msg);
break;
default:
navigator.notification.alert('EVENT -> Unknown, an event was received and we do not know what it is');
break;
}
}
function tokenHandler(result) {
navigator.notification.alert(result, null, 'Alert', 'OK');
sessionStorage.setItem("deviceId", result);
sessionStorage.setItem("notificationServer", "APNS");
// Your iOS push server needs to know the token before it can push to this device
// here is where you might want to send it the token for later use.
}
function successHandler(result) {
navigator.notification.alert(result, null, 'Alert', 'OK');
sessionStorage.setItem("deviceId", result);
sessionStorage.setItem("notificationServer", "GCM");
}
function errorHandler(error) {
navigator.notification.alert(error, null, 'Alert', 'OK');
}
document.addEventListener('deviceready', onDeviceReady, true);
</script>
<div id="home">
<div id="app-status-div">
<ul id="app-status-ul">
<li>Cordova PushNotification Plugin Demo</li>
</ul>
</div>
</div>
3 回答
正如Hanh Le所说,你需要从window-object访问ecb-callback
像这样
然后更换
同
编辑:这是你发布的整个代码我认为应该工作的方式编辑:
你可以将
onNotificationGCM
功能放在控制器之外 . 这是最适合我的解决方案 .创建外部js文件,例如:
push.js
将
onNotificationGCM
函数粘贴到push.js
内,不使用任何angularjs API,例如angular.module
或.controller
在iOS的情况下,我找到了这个解决方案 . 此问题的原因是在
PushPlugin.m
中callback
属性在杀死应用程序时设置为nil
. 因此,- (void)notificationReceived
回调中不满足line#202的检查 . 该问题的解决方法是在每次应用启动时打开推送通知时实例化PushPlugin
类 . 这可以通过在app start上注册推送通知插件来完成 . 每次你的应用程序启动时,在你的javascript中调用pushNotification.register
方法,这将实例化插件并分配相应的callback
插件 . 我已将以下代码添加到我的Ionic
app的app.run()
部分