Xcode、Parse - 处理远程通知

标签 xcode swift push-notification parse-platform

我正在尝试按照解析指南来处理我以 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
    }

应用程序现在没有崩溃,但我所做的更改没有生效。 json代码:

{
    "aps": {
         "badge": 10,
         "alert": "Test",
         "sound": "cat.caf"
    },
    "url": "http://www.google.com"
}

最佳答案

我猜你的问题出在这里:

launchOptions(UIApplicationLaunchOptionsRemoteNotificationKey) as NSDictionary 

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

launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as NSDictionary 

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

也就是说,launchOptions 可能为 nil,您应该在代码中添加该检查,并且尝试记录 launchOptions 并在此处发布结果。

您可以像这样检查 launchOptions 是否为 nil:

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

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

相关文章:

android - 向 Android 发送推送通知

iphone - 如何放大 Storyboard(xcode 6)

iphone - 应用程序在设备上崩溃,但在模拟器中推送 UITableView 时不会崩溃

ios - 如何在带有图像的 SwiftUI 中使用 Bundle

xcode - Swift 中递归引用自身的函数

ios - 将 nib 文件添加到 ScrollView 时如何删除底部空白

ios - 您可以在 Google Analytics for iOS 中跟踪的事件数有上限吗?

objective-c - po [NSThread 当前线程]

ios - 如果负载中缺少锁定 key ,推送通知是否会显示

ios - didRegisterForRemoteNotificationsWithDeviceToken 没有被调用 Swift 5,Xcode 10.2