【发布时间】:2012-08-20 10:10:46
【问题描述】:
当我的应用正在运行并收到推送通知时,如果我点击该通知,应用就会启动 - 但它不会提示用户使用我设置的警报视图,询问他们是否要查看通知的内容。它刚刚启动,然后就坐在那里。
当应用程序运行时,推送通知确实完美工作 - 无论是作为活动应用程序还是在后台运行 - 但没有任何效果当应用没有运行时正确。
我尝试在application: didFinishLaunchingWithOptions: 中注销launchOptions NSDictionary 以查看它带来了什么负载 - 但它显示为“(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 - 应用程序启动,但我根本没有收到推送通知警报它启动。它就在那里。
猜猜我接下来需要弄清楚那个排列...
【问题讨论】:
-
那么当您不点击推送通知并直接点击应用程序图标时,您是否能够收到通知?
-
你能做到吗? ?我的意思是当应用程序处于非运行状态时如何处理推送通知?如果您收到许多通知并且您没有打开应用程序,也没有点击系统的通知面板怎么办。您如何保留这些推送以供以后检索。
-
你有没有碰巧得到最后的排列工作?
-
这在 iOS 8.X 中有效吗?通过我的实验,除非用户点击通知,否则内容将永远丢失(即它不像它被存储并且可以在用户启动应用程序时检索)。此外,我看到的奇怪的事情是,即使我终止应用程序并在此之后单击传入通知,通知也会在 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler: (void (^)(UIBackgroundFetchResult 结果))handler
-
你解决了吗? @sirab33,我也需要回答这个问题吗?当应用程序未运行和通知进来时,我仍然无法获取数据,不点击它,仍然无法获取数据。任何帮助?
标签: iphone push-notification apple-push-notifications