Swift - How sending a post notification from OneSignal SDK to specific username tag?

陌路散爱 提交于 2019-12-06 04:07:12

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

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

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.

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.

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

You can find additional examples in the OneSignal documentation here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!