我有以下方法来处理推送通知。当应用程序运行时,它们可以完美运行,当应用程序在后台时,我会收到通知。当用户点击图标时,它会打开应用程序但我的UICollection视图不会像用户在应用程序运行时收到通知时那样重新加载。
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
// do stuff when app is active
NSString *cancelTitle = @"Close";
NSString *showTitle = @"Ok";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
NSString *type = [userInfo objectForKey:@"type"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Linkedstar"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
if([type isEqualToString:@"message"]) {
alertView.tag = alertMessage;
}
else if([type isEqualToString:@"post"]) {
alertView.tag = post;
}
else if([type isEqualToString:@"contact"]) {
alertView.tag = contact;
}
[alertView show];
[self presentViewForPush:userInfo updateUI:YES];
}
else
{
// do stuff when app is in background
NSLog(@"Received notification: %@", userInfo);
[self handlePush:userInfo updateUI:YES];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *pushDict = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(pushDict)
{
[self handlePush:pushDict updateUI:YES];
}
}
当用户点击图标时,我需要帮助弄清楚如何处理launchOptions数据。我在网上做了一些研究之后尝试了几种不同的选择。 任何帮助是极大的赞赏。谢谢。
答案 0 :(得分:3)
1) 当应用程序在后台运行时和当应用程序在前台运行时
application:didReceiveRemoteNotification:
方法将在下面调用。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateInactive)
{
//opened from a push notification when the app was on background
NSLog(@"userInfo->%@",[userInfo objectForKey:@"aps"]);
}
else if(application.applicationState == UIApplicationStateActive)
{
// a push notification when the app is running. So that you can display an alert and push in any view
NSLog(@"userInfo->%@",[userInfo objectForKey:@"aps"]);
}
}
2) 当应用程序未启动(关闭)时,将调用application:didFinishedLaunchWithOptions
方法。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (launchOptions != nil)
{
//opened from a push notification when the app is closed
NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo != nil)
{
NSLog(@"userInfo->%@",[userInfo objectForKey:@"aps"]);
}
}
else{
//opened app without a push notification.
}
}
答案 1 :(得分:1)
您是否已阅读此Scheduling, Registering, and Handling Notifications
如果点击操作按钮(在运行iOS的设备上),则系统 启动应用程序,应用程序调用其代理人 application:didFinishLaunchingWithOptions:方法(如果实现);它 传递通知有效载荷(用于远程通知)或 本地通知对象(用于本地通知)。
如果在运行iOS的设备上点按应用程序图标,则 应用程序调用相同的方法,但提供无信息 通知
答案 2 :(得分:0)
处理通知是在didReceiveRemoteNotification(app为“alive”)或didFinishLaunchingWithOptions(app为“dead”)中完成的。
如果用户只需点击图标,我认为您无法处理这些通知。他们必须自己完成通知。
答案 3 :(得分:0)
我遇到了同样的问题:如果用户点击推送横幅,他会获得推送应用的信息,如果他点击应用图标,他就不会得到它。你可以从中处理派生的,但只有一些限制。例如,如果您想要推送徽章编号,则可以执行以下操作: (推送 - >应用图标 - >应用图标徽章 - >您的var)
AppDelegate中的
- (void)applicationWillEnterForeground:(UIApplication *)application
{
newMessages = application.applicationIconBadgeNumber;
}