问题
I have the methods below to handle push notifications. They work perfect when the app is running and I do receive notifications when the app is in the background. When a user taps the icon however it opens the app but my UICollection view is not reloaded the way it is when a user receives the notification when the app is running.
-(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];
}
}
I need help figuring out how to handle the launchOptions data when the user taps on the icon.I have tried a couple of different options after doing some research online and non worked. any help is greatly appreciated. Thank you.
回答1:
Have you read this Scheduling, Registering, and Handling Notifications
If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).
If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification
回答2:
1) When application is running in background and When application is running in foreground
application:didReceiveRemoteNotification:
method will called as below.
- (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) When application is not launched (close) than application:didFinishedLaunchWithOptions
method will called.
- (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.
}
}
回答3:
Handling notifications is done in either didReceiveRemoteNotification (app is "alive") or didFinishLaunchingWithOptions (app was "dead").
I don't think you can handle these notifications if the user simply taps on the icon. They must go through the notification itself.
回答4:
I had the same problem: if user clicks on push banner he gets info of push in app, if he clicks on app icon he doesn't get it. You can handle derivative one from it, but with some limits only. Example, if you want to have a badge number from push, you can do it: (Push -> App icon -> App icon badge -> your var)
in AppDelegate
- (void)applicationWillEnterForeground:(UIApplication *)application
{
newMessages = application.applicationIconBadgeNumber;
}
来源:https://stackoverflow.com/questions/22977560/pushnotifications-ios-7-not-working-when-user-taps-on-icon