如何使用swift从launchOptions获取自定义推送通知值?

Aer*_*fan 8 notifications push ios swift

我正在使用Swift构建一个接收推送通知的应用程序.我在JSON中发送自定义值.

我通过通知打开应用程序,所以我知道我必须在"didFinishLaunchingWithOptions"中执行此操作并从"launchOptions"中读取值.

如何阅读这些值并在我的应用程序中使用它们.

非常感谢.

Ray*_*ion 3

在应用程序中:didReceiveRemoteNotification:fetchCompletionHandler,自定义数据被传递给didReceiveRemoteNotification,它是一个NSDictionary。您想要检索的详细信息可能位于 userInfo 的“aps”键上。

func application(application: UIApplication, didReceiveRemoteNotification userInfo: NSDictionary!)
{
    var notificationDetails: NSDictionary = userInfo.objectForKey("aps") as NSDictionary
}
Run Code Online (Sandbox Code Playgroud)

当应用程序未启动时,您需要从应用程序中获取:didFinishedLaunchWithOptions,

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

    if let launchOpts = launchOptions {
      var notificationDetails: NSDictionary = launchOpts.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey) as NSDictionary
    }

    return true
}
Run Code Online (Sandbox Code Playgroud)

编辑:远程通知修复语法