iamburak
iamburak

Reputation: 3568

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

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",
                ])
        }

    })

} 
})

Upvotes: 3

Views: 2246

Answers (1)

Gdeglin
Gdeglin

Reputation: 12618

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.

Upvotes: 1

Related Questions