iphone Launch Options

2020-02-10 07:01发布

问题:

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.

回答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:)



回答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



回答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)
    }


回答4:

If you want a local notification (I assume with your var name) replace UIApplicationLaunchOptionsRemoteNotificationKey by UIApplicationLaunchOptionsLocalNotificationKey and this should work.



回答5:

  (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
      NSLog(@"Alert message: %@",[[userInfo valueForKey:@"aps"] valueForKey:@"alert"]);
}


回答6:

You can use NSUserDefaults to do the trick. In your AppDeligate.m, set a bool to YES in the first time. So after that it never gets to NO.

  -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //add this if loop
        if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
        {
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    }