当我的应用程序是不运行并接收推送通知,如果我点击该通知,应用启动 - 但是它不与提示用户我建立了Alert-View,询问他们是否想查看Notification的内容。它刚刚启动,并坐在那里。处理推送通知时,应用程序无法运行
的推送通知做的工作完美当应用程序是运行 - 无论是作为活跃APP或者在后台 - 但没有正常工作时,应用程序是不运行。
我试图注销launchOptions
NSDictionary在application: didFinishLaunchingWithOptions:
看看是什么加载它的带来 - 但它出现为“(空)”。所以它基本上不包含任何内容 - 这不合理,因为它不应该包含通知的负载?
任何人有任何想法如何使推送通知的工作,当他们到达时,应用程序没有运行?
编辑:这里是我使用的application: didReceiveRemoteNotification
只是为了看看什么是什么代码:
if (UIApplicationStateBackground) {
NSLog(@"===========================");
NSLog(@"App was in BACKGROUND...");
}
else if (UIApplicationStateActive == TRUE) {
NSLog(@"===========================");
NSLog(@"App was ACTIVE");
}
else {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 99];
UIAlertView *BOOM = [[UIAlertView alloc] initWithTitle:@"BOOM"
message:@"app was INACTIVE"
delegate:self
cancelButtonTitle:@"a-ha!"
otherButtonTitles:nil];
[BOOM show];
NSLog(@"App was NOT ACTIVE");
}
所以这是应该采取应用程序的所有国家的照顾 - 但它不是。推送通知只适用于应用程序运行时 - 无论是在后台还是在前台...
======================== ========================
UPDATE/EDIT#2: 根据“@dianz”建议(下文)我修改了代码我application: didFinishLaunchingWithOptions
的,包括以下内容:
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
NSString *json = [localNotif valueForKey:@"data"];
UIAlertView *bam = [[UIAlertView alloc] initWithTitle:@"appDidFinishWithOptions"
message:json
delegate:self
cancelButtonTitle:@"cool"
otherButtonTitles:nil];
[bam show];
}
这确实让AlertView框出现,但似乎没有有效载荷:该AlertView的标题显示出来(“appDidFinishWithOptions”),但JSON的NSString出现EMPTY ...将继续调整...
======================
编辑#3 - 其现在的工作几乎 100%
所以,在didFinishLaunchingWithOptions
:
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
// Grab the pushKey:
pushKey = [localNotif valueForKey:@"pushKey"];
// "pushKey" is an NSString declared globally
// The "pushKey" in the "valueForKey" statement is part of my custom JSON Push Notification pay-load.
// The value of pushKey will always be a String, which will be the name of the
// screen I want the App to navigate to. So when a Notification arrives, I go
// through an IF statement and say "if pushKey='specials' then push the
// specialsViewController on, etc.
// Here, I parse the "alert" portion of my JSON Push-Notification:
NSDictionary *tempDict = [localNotif valueForKey:@"aps"];
NSString *pushMessage = [tempDict valueForKey:@"alert"];
// Finally, ask user if they wanna see the Push Notification or Cancel it:
UIAlertView *bam = [[UIAlertView alloc] initWithTitle:@"(from appDidFinishWithOptions)"
message:pushMessage
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"View Info", nil];
[bam show];
}
我接下来实现了alertView: clickedButtonAtIndex
方法,以查看用户在AlertView上选择的内容并据此进行操作。
这与逻辑didReceiveRemoteNotification
一起完美工作。然而,当应用程序没有运行,并且我发送推送通知时,如果我一到它就不点击推送通知警报,而是等待它淡出(发生这种情况在3-4秒之后),然后然后我点击应用程序的图标 - 该图标现在的BADGE为1 - 应用程序启动,但在启动时我没有收到推送通知警报。它只是坐在那里。
想我需要弄清楚是排列在下...
所以是你能得到通知时,你没有点击推送通知,并直接点击应用程序图标? –
你有这个工作。 ?我的意思是当应用程序处于非运行状态时如何处理推送通知?如果如果您收到很多通知并且您没有打开该应用程序,那么您也不会点击系统的通知面板。你如何保留这些推动以便以后检索。 –
你碰巧得到最后的排列工作吗? –