iphone Launch Options

22,183

Solution 1

Your application receives push notifications through multiple paths, depending on the state of your app when it is received.

If your app is not launched (not even suspended in background), the launchOptions will contain the notification payload (key UIApplicationLaunchOptionsRemoteNotificationKey).

If it is already running or suspended in background, the app will receive the notifications via application:didReceiveRemoteNotification: in your application delegate.

The process is the same for local notifications (UIApplicationLaunchOptionsLocalNotificationKey in application:didFinishLaunchingWithOptions: and application:didReceiveLocalNotification:)

Solution 2

an error spotted:

NSDictionary *remoteNotif = [launchOptions objectForKey:   
                              UIApplicationLaunchOptionsRemoteNotificationKey];

If you want to receive the remote notification, NSDictionary should be used not UILocalNotification

The remote notification is a payload, containing arguments, not a local notification. You might want to look at this url question:

Crash when handling remote notification when app not running

If you want to do local notification, change it like Ludovic's suggestion

Solution 3

The answers given above are correct. I largely use the following snippet in my application:didFinishLaunchingWithOptions:

if let remoteNotifPayload = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
        notificationsController.didReceiveRemoteNotification(with: remoteNotifPayload)
    }
Share:
22,183
user633268
Author by

user633268

Updated on August 23, 2022

Comments

  • user633268
    user633268 over 1 year

    im not getting any launch options after a push notification; heres the code i have but non of the NSLogs seem to print in the debug area.

    UILocalNotification *localNotif =
    [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    
    if (localNotif) {
        NSString *itemName = [localNotif.userInfo objectForKey:@"aps"];
        NSLog(@"Custom: %@", itemName);
    } else {
        NSLog(@"//////////////////////////");
    
    }
    

    when i open the app (via pressing view on the push notification) it goes to the didReceiveRemoteNotification script, im not sure if thats meant to happen..

    thanks for reading.

  • user633268
    user633268 about 13 years
    thanks, so is the anything so when the application is in the background the app can get the same result... i want it to change the page when the token custompage is page 2 or 3...
  • Jilouc
    Jilouc about 13 years
    You can peform the same actions in application:didFinishLaunchingWithOptions: than in application:didReceiveRemoteNotification:. Just keep in mind that in the 1st case, your app just started so you know perfectly what is displayed and in the 2nd case, the user could be anywhere in your app.
  • user633268
    user633268 about 13 years
    ok thanks, one more question how would you recommend i change the view? i got told to do NSUserDefaults but i would want something that would go to it right away. and if the user is on that page then refresh it.