我正在使用
Swift构建一个接收推送通知的应用程序.我在JSON中发送自定义值.
我通过通知打开应用程序,所以我知道我必须在“didFinishLaunchingWithOptions”中执行此操作并从“launchOptions”中读取值.
如何阅读这些值并在我的应用程序中使用它们.
非常感谢.
解决方法
当您的应用未启动时,这是适用于SWIFT 2的功能.由于可选的绑定,代码不是很优雅.但它的确有效.
- func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
- // if launched from a tap on a notification
- if let launchOptions = launchOptions {
- if let userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] {
- if let action = userInfo["action"],id = userInfo["id"] {
- let rootViewController = self.window!.rootViewController as! ViewController
- let _ = setTimeout(5.0,block: { () -> Void in
- rootViewController.openNotification(action as! String,id: id as! String)
- })
- }
- }
- }
- return true
- }