ios - 如何发布 One Signal 通知的附加数据并接收这些数据?

标签 ios swift push-notification onesignal

我检查了 OneSignal 文档,但作为初学者我无法清楚地理解如何在 iOS Native SDK 中使用 Swift 将字典设置为发布通知的附加数据(如 postID、userID、类型),以便在用户与通知交互时决定和重定向。

对于发布,我只是这样做的:

 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])

最佳答案

您将 data 字段设置为传递给 OneSignal.postNotification 的字典中的键,如下所示。

OneSignal.postNotification(["contents": ["en": "Test Message"],
                            "include_player_ids": ["3009e210-3166-11e5-bc1b-db44eb02b120"],
                            "data": ["postID": "id"]])

然后您需要准备好来自 handleNotificationAction 函数中 payloadadditionalData 的 key 。

if let additionalData = payload?.additionalData {
   let postID: String? = additionalData["postID"]
}

关于ios - 如何发布 One Signal 通知的附加数据并接收这些数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41634759/

相关文章:

ios - UICollectionView 间距每隔一行不正确

ios - iOS 上的 Realm : crash while creating nested object with primaryKey

ios - 我如何在同一个项目中拥有 iOS 应用程序和 OS X 应用程序?

android - OnTokenRefresh 从未在 Xamarin Android 中调用

ios - 获取音频m4a文件ios的播放时间

ios - 导航栏中的 SearchController 问题

ios - 如何在 uitableviewcell 内绘制一个矩形

ios - 无法读取数据,因为它的格式不正确 [swift 3]

ios - 后台任务

reactjs - 您的浏览器不支持 Firebase 消息传递,如何解决这个问题?