Search code examples
objective-cios7apple-push-notificationsnsnotificationcenternsnotifications

PushNotifications iOS 7 not working when user taps on icon


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.


Solution

  • 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