问题
OK, so i've read various articles on how to check the local notifications in didfinishlaunchingwith options. For example this NSHipster article claims that remote and local keys both contain an NSDictionary. http://nshipster.com/launch-options/
However, i tested and it contains a UILocalNotification, and some other articles says so as well.
So, i've looked around but not found any definitive answer. Is this an OS Version issue? Do different versions contain different objects, or what?
Pointers much appreciated.
EDIT:
From the NSHipster article:
"A local notification populates the launch options on UIApplicationLaunchOptionsLocalNotificationKey, which contains a payload with the same structure as a remote notification:
UIApplicationLaunchOptionsLocalNotificationKey: Indicates that a local notification is available for the app to process. The value of this key is an NSDictionary containing the payload of the local notification."
回答1:
According to Apple Documentation, the UIApplicationLaunchOptionsLocalNotificationKey will give you an UILocalNotification object.
If the default action button is tapped (on a device running iOS), the system launches the app and the app calls its delegate’s application:didFinishLaunchingWithOptions: method, passing in the notification payload (for remote notifications) or the local-notification object (for local notifications). Although application:didFinishLaunchingWithOptions: isn’t the best place to handle the notification, getting the payload at this point gives you the opportunity to start the update process before your handler method is called.
Sample code from Apple Docs
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSString *itemName = [localNotif.userInfo objectForKey:ToDoItemKey];
[viewController displayItem:itemName]; // custom method
app.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber-1;
}
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
Edit: Update for UIApplicationLaunchOptionsRemoteNotificationKey
UIApplicationLaunchOptionsRemoteNotificationKey
, returns NSDictionary with notification payload
also from the apple doc
The value of this key is the UILocalNotification object that was triggered. For additional information about handling local notifications, see the application:didReceiveLocalNotification: method.
apple-doc-UIApplicationLaunchOptionsLocalNotificationKey
Note: this key deprecated since iOS10.0
回答2:
The Options parameter in the didFinishLaunchingWithOptions:
is a dictionary, in which a UILocalNotification
can be contained as value for the key UIApplicationLaunchOptionsLocalNotificationKey
, as per the UIApplicationDelegate.
This is different from Remote notifications, which is a NSDictionary
containing the payload, and can be fetched with the UIApplicationLaunchOptionsRemoteNotificationKey
key.
UIApplicationLaunchOptionsRemoteNotificationKey
The presence of this key indicates that a remote notification is available for the app to process. The value of this key is an NSDictionary containing the payload of the remote notification. See the description of application:didReceiveRemoteNotification: for further information about handling remote notifications.
UIApplicationLaunchOptionsLocalNotificationKey
The presence of this key indicates that a local notification is available for the app to process. The value of this key is the UILocalNotification object that was triggered. For additional information about handling local notifications, see the application:didReceiveLocalNotification: method.
来源:https://stackoverflow.com/questions/35034074/does-launchoptions-uiapplicationlaunchoptionslocalnotificationkey-contain-nsdi