我正在尝试遵循“解析指南”来处理我以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
    }

该应用程序现在不会崩溃,但是我所做的更改不会发生。
杰森代码:
{
    "aps": {
         "badge": 10,
         "alert": "Test",
         "sound": "cat.caf"
    },
    "url": "http://www.google.com"
}

最佳答案

我猜你的问题在这里:

launchOptions(UIApplicationLaunchOptionsRemoteNotificationKey) as NSDictionary

我不确定您的期望如何,但据我所知,字典上没有类似的方法。您可能正在寻找下标语法。像这样的东西:
launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as NSDictionary

要获得嵌套在launchOptions词典中的词典,其键为:UIApplicationLaunchOptionsRemoteNotificationKey。

话虽这么说,launchOptions可能为零,您应该在代码中添加该检查,并尝试记录launchOptions并将结果发布到此处。

您可以像这样检查launchOptions是否为nil:
if let launchOpts = launchOptions {
    var notificationPayload: NSDictionary = launchOpts.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey) as NSDictionary
}

关于xcode - Xcode,解析-处理远程通知,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26244474/

10-13 05:46