I'm using onesignal to handle pish notification for both my web and ionic app. My question is
Can I setup user segments using server rest api ?
I want to give my users ability to subscribe to specific events (channels) to limit push notifications they receive.
Example
user1 subscribed to group I'd (1,3,8)
user2 groups(5,8,11)
User3 groups(12,13)
When event happened on groups 8.
How can I send push notifications to all users subscribed to group 8 ? -in my example above notification should be sent to user1 & user2 but not 3.
OneSignal doesn't currently support dynamically creating segments through the API, however there is a better way to do what you need.
You can use the tags feature to assign custom data to users, and then you can deliver notifications to users who match specific tags. Tags are typically set by calling the SetTags method in your app, but they can also be set through the OneSignal API.
In your case, for instance, you would assign the following tags to each user:
User1 Tags: group1=true,group3=true,group8=true
User2 Tags: group5=true,group8=true,group3=true
User3 Tags: group12=true,group13=true
Next, through the OneSignal API, you can use the "tags" field to only deliver your notifications to users who match one or more tags.
You could also create a segment for each tag on the dashboard, then through the API or through the dashboard you could specify which segments (and therefore tags) should receive your notification.
The problem with the approach outlined by @Gdeglin is that you will need to manage your group information on both your server and OneSignal's. When you have 2 sources of truth for something as complex and dynamic as groups you're bound to run into trouble.
For example, what happens if Group 8 is deleted? In addition to updating your own database, you'd need to use the API to update all the OneSignal users who belonged to Group 8. Imagine that group contains 1000 users. Yuck.
Instead of adding groupname tags, add a "userId" tag to each user (Player) registered with OneSignal. Now when a group changes there is nothing to update with OneSignal.
The tradeoff is that sending a message to everyone in the group requires N API calls to OneSignal instead of just one. It looks like it's possible to supply multiple tag matches in a single API call, but it's not clear how well that scales.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With