当我的应用程序是not运行并收到推送通知,如果我单击该通知,应用程序将启动 - 但随后它不会提示用户我设置的警报视图,询问他们是否要查看通知的内容。它刚刚启动,就坐在那里。
推送通知确实有效完美当应用程序is正在运行 - 无论是作为活动应用程序还是在后台运行 - 但当应用程序运行时,一切都无法正常运行not跑步。
我尝试注销launchOptions
NS字典中application: 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 - 它现在正在工作almost 100%
So, in 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-3 后发生) 4 秒),以及then我单击应用程序的图标 - 现在其上的 BADGE 为 1 - 应用程序启动,但在启动时我根本没有收到推送通知警报。它就坐在那里。
我想我需要弄清楚that接下来排列...