-2

I'm trying to figure out how I can examine the payload of a push notification in order to determine which view opens when a user opens the app from the notification. For example, if a notification says "x: test" view x would open when the notification is tapped and if the notification says "y: test" view y would open.

EDIT: I guess I should clarify the part I'm not sure about.

I have this in didFinishLaunchingWithOptions:

UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    if (notification)
    {
        // check payload then load appropriate view controller
    }

How would I check the payload for certain text in order to determine the appropriate view controller to load?

raginggoat
  • 3,570
  • 10
  • 48
  • 108
  • Have you done any search? In application didFinishLaunchingWithOptions method in AppDelegate.m you read the remote notification and depending on what is "says" with an if-statement you open the correct view. – Kostis Nov 04 '14 at 20:23
  • https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html It seems that you are trying to do something similar to the solution in http://stackoverflow.com/questions/8685333/how-to-manage-notification-when-users-click-on-badge – propstm Nov 04 '14 at 20:50

2 Answers2

1

Receiving a push notification is handled in two places in the app delegate

  • - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions when your app is launched from a push notification. In this case, your push notification data is contained within [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]

  • - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo when your app is running when receiving the notification. In this case, userInfo is your push notification data.

bengoesboom
  • 2,119
  • 2
  • 23
  • 27
1

Here is my code from a past project. The notification shows up in device like this "Kostas: wants to add you as friend".

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
 (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

NSDictionary *remoteNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if (remoteNotif) {

    NSString *name = [[NSString alloc] init];
    NSString *message = [[NSString alloc] init];
    // 'wants to add you as friend.'
    NSString* alertValue = [[remoteNotif valueForKey:@"aps"] valueForKey:@"alert"];

    NSMutableArray* parts = [NSMutableArray arrayWithArray:[alertValue componentsSeparatedByString:@": "]];
    name = [parts objectAtIndex:0];
    [parts removeObjectAtIndex:0];
    message = [parts componentsJoinedByString:@": "];

    if ([message isEqualToString:@": wants to add you as friend."]) {
        UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
        // tabController.delegate = self;
        tabController.selectedIndex = 1;
    }
    else{
        UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
        // tabController.delegate = self;
        tabController.selectedIndex = 2;

        [self addMessageFromRemoteNotification:remoteNotif updateUI:NO];
    }
Kostis
  • 953
  • 9
  • 21