您好,感谢您查看这个问题,我在iOS应用程序打开时接收推送通知有问题。当应用程序在后台运行时,它工作完美。但是当应用程序打开时,它甚至没有触发'push-notification'事件,该事件应该将通知显示为警报消息。
我遵循这些指示,但做了一些调整:http://www.pushwoosh.com/programming-push-notification/ios/ios-additional-platforms/push-notification-sdk-integration-for-phonegap/
,这里是我的代码当页面加载:
document.addEventListener("deviceready", pushwooshReady, true);
document.addEventListener("push-notification", displayPushwooshMessage, true);
pushwooshReady 功能:
function pushwooshReady() {
initPushwoosh();
if(device.platform == 'iOS') {
app.receivedEvent('deviceready');
}
}
initPushwoosh函数:
function initPushwoosh() {
//get the plugin
var pushNotification = window.pushNotification;
if(device.platform == 'iOS') {
//call the registration function for iOS
registerPushwooshIOS();
} else if (device.platform == 'Android') {
//call the registration function for Android
registerPushwooshAndroid();
}
pushNotification.onDeviceReady();
}
,这是registerPushWooshIOS函数:
function registerPushwooshIOS() {
var pushNotification = window.pushNotification;
//register for push notifications
pushNotification.registerDevice({alert:true, badge:true, sound:true, pw_appid: PW_appid, appname: PW_appname},
function(status) {
//this is a push token
var deviceToken = status['deviceToken'];
console.warn('registerDevice: ' + deviceToken);
//we are ready to use the plugin methods
onPushwooshiOSInitialized(deviceToken);
},
function(status) {
console.warn('failed to register : ' + JSON.stringify(status));
navigator.notification.alert(JSON.stringify(['failed to register ', status]));
});
//reset badges on application start
pushNotification.setApplicationIconBadgeNumber(0);
}
这里是我的displayPushwooshMessage函数:
function displayPushwooshMessage(event) {
if(device.platform == 'Android') {
var msg = event.notification.title;
var userData = event.notification.userdata;
if(typeof(userData) != "undefined") {
console.warn('user data: ' + JSON.stringify(userData));
}
alert(msg);
} else if(device.platform == 'iOS') {
var notification = event.notification;
alert(notification.aps.alert);
pushNotification.setApplicationIconBadgeNumber(0);
}
}
非常感谢您的帮助
好了,看来我又要回答我自己的问题了。
我使它工作,但没有使用javascript的alert()方法,因为在pushwoosh网站上的说明并不是真的为我工作。所以我所做的很简单,但花了我一天的时间才弄清楚,因为我没有任何Objective-C背景。
我注释掉了PushNotificationManager.m中的handlePushReceived函数中的if语句。下面是代码:
NSString *alertMsg = [pushDict objectForKey:@"alert"];
bool msgIsString = YES;
if(![alertMsg isKindOfClass:[NSString class]])
msgIsString = NO;
//if(!isPushOnStart && showPushnotificationAlert && msgIsString) { <- Commented this out
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:self.appName message:alertMsg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
alert.tag = ++internalIndex;
[pushNotifications setObject:userInfo forKey:[NSNumber numberWithInt:internalIndex]];
[alert show];
return YES;
//} <- Also this one.
[self processUserInfo:userInfo];
它起作用了。我甚至不知道为什么它不工作,它是从我下载的SDK。哦,好吧,我不确定是否有人遇到过或会遇到同样的问题,但我希望这个答案能帮助那些将来会遇到的人。
* *更新:
实际上,错误是不同的,不需要上面的解决方案。我收到Pushwoosh支持团队的以下回复:
Dmitry Dyudeev (Pushwoosh)
5月29日16:41你好克林特,
感谢您与我们联系!
你发现了我们插件中的一个bug。谢谢你指出来!我们将在最近的将来更新我们的插件。
同时,由于更新插件需要一些时间,你可以在PushNotification中找到onDeviceReady方法。M文件并在其末尾添加以下行,它将解决问题:
[[NSUserDefaults standardUserDefaults] synchronize];
请让我知道结果!
问候,德米特里•Dyudeev
Pushwoosh团队