微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/iOS/334539.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐