【问题标题】:Xcode, Parse - Handling remote notificationsXcode, Parse - 处理远程通知
【发布时间】:2014-12-02 09:07:05
【问题描述】:

我正在尝试按照 Parse Guide 来处理我以 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"
}

【问题讨论】:

    标签: xcode swift push-notification parse-platform


    【解决方案1】:

    我猜你的问题在这里:

    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
    }
    

    【讨论】:

    • 请检查我更新的帖子,如果 launchoptions == nil 怎么办?
    • 更新了如何检查 nil,如果它们是 nil,我不确定你应该怎么做。我假设如果它们为零,则应用程序没有通过通知打开,您应该继续。
    • 这行得通,但现在我无法让通知影响我的应用程序。请检查我的编辑。
    • 我不确定您所说的“影响我的应用”是什么意思。这是您在打开应用程序时响应通知的地方。例如,如果用户点击了您的通知,您可以使用它来解释被点击的通知并在您的应用程序中做出相应的响应。您希望发生哪些没有发生的事情?
    • 看看我上面的代码,我正在尝试发送一个字符串以 json 格式添加到我的表中,我的手机接收并显示消息,但我的表永远不会更新或改变了。
    【解决方案2】:

    NSDictionary 的 objectForKey 返回一个可选的:

    func objectForKey(_ aKey: AnyObject) -> AnyObject?

    所以如果你使用 !如果 Optional 包含 nil,您将承担风险。仅当您 100% 确定 Optional 包含值时才应强制展开,但在这种情况下,UIApplicationLaunchOptionsRemoteNotificationKey 仅在用户点击通知中心中的远程通知消息时启动应用程序时设置。

    您必须使用 as? 检查 Optional 并向下转换为 NSDictionary:

    
    if let myDict = launchOptions[UIApplicationLaunchOptionsRemoteNotifcationKey] as? NSDictionary {
    // there is a notification
    } else {
    // no notification
    }
    

    (“as?NSDictionary”不是严格要求的,因为您可以稍后进行向下转换;如果您不向下转换,Swift 会警告您 myDict 对象将被推断为 AnyObject。

    【讨论】:

      【解决方案3】:
      if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
      
      }
      

      【讨论】:

        猜你喜欢
        • 2012-12-12
        • 1970-01-01
        • 2017-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多