当应用程序在运行时 - 无论是作为活动应用程序还是在后台运行时 - 推送通知都能正常工作,但是应用程序未运行时没有任何东西能正常工作。
我尝试在
application: didFinishLaunchingWithOptions:
中注销launchOptions
NSDictionary以查看它带来了什么负载-但它显示为空值。因此,它基本上不包含任何内容-这没有意义,因为它不应该包含通知的负载吗?有人有什么想法可以使推送通知在应用程序未运行时到达时起作用?
编辑:这是我在
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");
}
这个代码本应该负责处理应用程序的所有状态——但它并没有。推送通知仅在应用程序在前台或后台运行时才能正常工作...
================================================
更新/编辑#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为空...会继续调整...
======================
编辑#3-它现在工作得几乎完美
所以,在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秒后),然后我点击应用程序的图标 - 现在它有1个标记,应用程序启动,但当它启动时,我根本没有收到推送通知警报。它只是坐在那里。
我猜我需要找出那个排列组合……
didRecieveRemoteNotificaiton
。如果您收到许多通知并且没有点击,则只需按照我之前说的做即可。 - mfaani