如何发布一个信号通知的其他数据并接收该数据? [英] How posting One Signal notification's additional data and receiving that?

查看:106
本文介绍了如何发布一个信号通知的其他数据并接收该数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I checked OneSignal documentation but I couldn't understand clearly as beginner how setting dictionary as a post notification's additional data (like postID, userID, type) in iOS Native SDK using Swift to decide and redirect when user interacted with notification.

对于发布,我只这样做:

For posting I'm doing only like that:

 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的字典中的键,如下所示.

You set the data field as a key in the dictionary passed to OneSignal.postNotification like the following.

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

然后,您需要从handleNotificationAction函数中的payloadadditionalData准备键.

Then you need to get ready your keys from additionalData from the payload in the handleNotificationAction function.

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

这篇关于如何发布一个信号通知的其他数据并接收该数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆