ios – 如何发布One Signal通知的附加数据并接收该数据?

前端之家收集整理的这篇文章主要介绍了ios – 如何发布One Signal通知的附加数据并接收该数据?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我检查了OneSignal文档,但我无法理解为初学者如何在iOS Native SDK中使用 Swift设置字典作为帖子通知的附加数据(如postID,userID,type),以便在用户通知交互时进行决定和重定向.

对于发布我只是这样做:

OneSignal.sendTag("username",value: "\(user)")
 OneSignal.postNotification(["contents": ["en": "@\(user) added an additive to your '\(title)' experience: \"\(strLast)\""],"include_player_ids": [postOwnerPlayerID],

接收:

OneSignal.initWithLaunchOptions(launchOptions,appId: "______",handleNotificationReceived: nil,handleNotificationAction: {
        (result) in

        // This block gets called when the user reacts to a notification received
        let payload = result?.notification.payload

        //Try to fetch the action selected
        if let additionalData = payload?.additionalData {

            print("payload")
            print(additionalData)
        }

        // After deciding which action then I can redirect user..

        let username: String? = UserDefaults.standard.string(forKey: KEY_UID)

        if username != nil {

            if let tabbarController = self.window!.rootViewController as? UITabBarController {
                tabbarController.selectedViewController = tabbarController.viewControllers?[2]
                // NotificationCenter.default.post(name: Foundation.Notification.Name(rawValue: "notificationsUp"),object: nil)
            }
        }

    },settings: [kOSSettingsKeyInFocusDisplayOption : OSNotificationDisplayType.none.rawValue])

解决方法

您将数据字段设置为传递给OneSignal.postNotification的字典中的键,如下所示.
OneSignal.postNotification(["contents": ["en": "Test Message"],"include_player_ids": ["3009e210-3166-11e5-bc1b-db44eb02b120"],"data": ["postID": "id"]])

然后,您需要从handleNotificationAction函数中的payload中的additionalData准备好您的密钥.

if let additionalData = payload?.additionalData {
   let postID: String? = additionalData["postID"]
}
原文链接:https://www.f2er.com/iOS/334539.html

猜你在找的iOS相关文章