App未运行时处理推送通知
当我的应用没有运行并且收到推送通知时,如果我点击该通知,该应用就会启动 - 但它不会提示用户使用我设置的Alert-View,询问他们是否要查看通知的内容与否。 它刚刚启动,并坐在那里。
当应用程序正在运行时,推送通知可以完美工作 - 既可以作为活动应用程序,也可以在后台运行 - 但在应用程序未运行时没有任何工作正常。
我尝试在application: didFinishLaunchingWithOptions:
launchOptions
NSDictionary application: didFinishLaunchingWithOptions:
查看加载它的是什么 - 但它是以“(null)”的形式出现的。 所以它基本上不包含任何内容 - 这不合理,因为它不应该包含通知的负载?
任何人有任何想法如何使推送通知的工作,当他们到达时,应用程序没有运行?
编辑:这里是我在application: didReceiveRemoteNotification
使用的代码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
代码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 - 应用程序启动,但启动时我没有收到推送通知警报。 它只是坐在那里。
猜猜我需要确定下一个排列...
当你的应用程序没有运行或死亡,你点击推送通知,这个功能将触发;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
所以你应该像这样处理它,
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
NSString *json = [localNotif valueForKey:@"data"];
// Parse your string to dictionary
}
如果你重写didReceiveRemoteNotification
那么你最好去
NSDictionary * pushNotificationPayload = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(pushNotificationPayload) {
[self application:application didReceiveRemoteNotification:pushNotificationPayload];
}
这将再次触发didReceiveRemoteNotification
方法,并且您的push notification
代码将与在应用程序运行时运行时相同的方式执行。
我试过@ dianz的回答,这对我没有帮助,但我从答案中得到了帮助。
在我的情况下,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *apnsBody = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (apnsBody) {
// Do your code with apnsBody
}
链接地址: http://www.djcxy.com/p/67253.html