Swift-如何从OneSignal SDK向特定的用户名标签发送发布通知? [英] Swift - How sending a post notification from OneSignal SDK to specific username tag?

查看:156
本文介绍了Swift-如何从OneSignal SDK向特定的用户名标签发送发布通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Swift/iOS本机SDK向特定标签(如(用户名:"@ john"))发送后期通知.我之前有sendTag OneSignal.我在下面发送一个具有playerID的用户.但这还不够.有人可以使用不同的帐户登录.因此,相同的playerID将适用于所有这些玩家.当我添加标签行时,它无法按预期工作.播放器ID和&&的工作方式用户名标签.

How Can I send a post notification to specific tag like (username: "@john") from Swift/iOS Native SDK. I have sendTag OneSignal before. I'm sending below a user with playerID. But It's not enough. Someone can login with different accounts. So same playerID will be for all of them. When I add tag line it doesn't work as I expect. How to work Player ID && username tag together.

OneSignal.sendTag("username", value: "\(username)")

如果有人解释这一点,那就太好了.

If someone explain this, It would be great.

DataService.ds.REF_USERS.child(userUUID).child("playerID").observeSingleEvent(of: .value, with: { snapshot in

if let playerID = snapshot.value as? String {

    OneSignal.idsAvailable({ (userId, pushToken) in
        print("UserId:%@", userId ?? "")

        self.showErrorAlert("Player ID Here:", msg: "\(userId!)")

        if (pushToken != nil) {

            OneSignal.postNotification(["contents": ["en": "@username wrote a comment: \(trimmed)"],
                                        "include_player_ids": [playerID],
                                        "send_after": "2017-01-10 20:10:00 GMT+0300",
                                        "tag": ["field": "tag", "key": "username", "relation": "=", "value": "john",
                ])
        }

    })

} 
})

推荐答案

出于安全原因,OneSignal不允许您在应用程序代码中使用标签定位.

For security reasons, OneSignal does not allow you to use tag targeting from within your app code.

不过,OneSignal确实允许您在应用程序内使用include_player_ids字段来定位设备.

OneSignal does allow you to target devices using the include_player_ids field from within your app though.

请注意,OneSignal也不允许您组合不同的定向参数.因此,您可能不能同时使用include_player_idstags定位.

Note that OneSignal also does not allow you to combine different targeting parameters. So you may not use both include_player_ids and tags targeting together.

要基于用户名发送通知,请使用OneSignal.sendTag方法分配用户名,如您的示例所示.接下来,在服务器端代码中,使用filters定位参数将通知发送给具有该用户名的所有用户.

To send notifications based on username, use the OneSignal.sendTag method to assign the username like in your example. Next, from your server-side code, use the filters targeting parameter to send a notification to all users with that username.

例如,下面是在Ruby中执行此操作的示例:

For instance, here is an example of how to do this in Ruby:

params = {"app_id" => "5eb5a37e-b458-11e3-ac11-000c2940e62c", 
          "contents" => {"en" => "English Message"},
          "filters" => [
                        {"field": "tag", 
                         "key": "username", 
                         "relation": "=", 
                         "value": "(username)"}
                       ]
        }
uri = URI.parse('https://onesignal.com/api/v1/notifications')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

request = Net::HTTP::Post.new(uri.path,
                              'Content-Type'  => 'application/json;charset=utf-8',
                              'Authorization' => "Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj")
request.body = params.as_json.to_json
response = http.request(request) 
puts response.body

您可以在OneSignal文档中找到其他示例

You can find additional examples in the OneSignal documentation here.

这篇关于Swift-如何从OneSignal SDK向特定的用户名标签发送发布通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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