当我的应用程序未运行并收到推送通知时,如果我单击该通知,则会启动应用程序 - 但它不会提示用户使用我设置的警报视图,询问他们是否要查看通知的内容与否。它刚刚启动,并坐在那里。
推送通知在应用程序运行时可以完美运行——无论是作为活动应用程序还是在后台运行——但当应用程序不运行时,没有任何东西可以正常工作。
我尝试注销launchOptions
NSDictionaryapplication: didFinishLaunchingWithOptions:
以查看它带来的负载 - 但它显示为“(null)”。所以它基本上什么都不包含 - 这没有意义,因为它不应该包含通知的负载吗?
任何人都知道如何在应用程序未运行时使推送通知到达时工作?
编辑:这是我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 出现 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 - 应用程序启动,但在启动时我根本没有收到推送通知警报。它只是坐在那里。
我想我接下来需要弄清楚这个排列......