Xcode,Parse - 处理远程通知

Abd*_*023 6 xcode push-notification parse-platform swift

我正在尝试按照Parse指南来处理我以Json格式发送的通知,但我遇到了问题,这是我的代码:

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
        // Override point for customization after application launch.
        Parse.setApplicationId("MyAppId", clientKey: "MyAppClientKey")

        var notificationType: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound

        var settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationType, categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(settings)
        UIApplication.sharedApplication().registerForRemoteNotifications()

if let launchOptions = launchOptions {
    var notificationPayload: NSDictionary = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as NSDictionary!
    println(launchOptions)

    var url = notificationPayload["url"] as String

    var feed: FeedTableViewController = FeedTableViewController()
    feed.messages.insert(url, atIndex: 0)
    feed.sections.insert("section", atIndex: 0)
    }


        return true
    }
Run Code Online (Sandbox Code Playgroud)

该应用程序现在不会崩溃,但我所做的更改不会发生.Json代码:

{
    "aps": {
         "badge": 10,
         "alert": "Test",
         "sound": "cat.caf"
    },
    "url": "http://www.google.com"
}
Run Code Online (Sandbox Code Playgroud)

Log*_*gan 9

我猜你的问题在这里:

launchOptions(UIApplicationLaunchOptionsRemoteNotificationKey) as NSDictionary 
Run Code Online (Sandbox Code Playgroud)

我不确定你在期待什么,但据我所知,在字典上没有这样的方法.您可能正在寻找下标语法.像这样的东西:

launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as NSDictionary 
Run Code Online (Sandbox Code Playgroud)

要获取嵌套在键下的launchOptions字典中的字典:UIApplicationLaunchOptionsRemoteNotificationKey.

话虽这么说,launchOptions可能是零,你应该在你的代码中添加该检查,并尝试记录launchOptions并在此处发布结果.

您可以检查launchOptions是否为零:

if let launchOpts = launchOptions {
    var notificationPayload: NSDictionary = launchOpts.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey) as NSDictionary
}
Run Code Online (Sandbox Code Playgroud)