23

I want onesignal to be able to send push notification to a specific device.

  1. How do I capture the device id of the device ?

  2. And what API do I use to push to that specific device ?

Thanks.

Axil
  • 3,606
  • 10
  • 62
  • 136

1 Answers1

17

Here is a copy of OneSignal's guide on sending a notification to an individual device:

If you're looking to send notifications to only your test devices:

  1. Tag your devices with a unique tag with sendTag.

    • Example: OneSignal.sendTag("is_test", "true")
    • For an exact example see OneSignal's API Reference for the SDK you're using.
  2. Create a new Segment on the OneSignal dashboard named test devices.

  3. Press "Add Filter" and select "User Tag" as the "Filter Type".
  4. Enter is_test equal to true.
    • The values you used in step 1.
  5. When creating a new notification, remove "All" and add your test devices segment.

If you're looking to send notifications to a specific user device: (Can be used for User-to-User notifications)

  1. Get the user's player_id with the getIdsAvailable SDK method.
    • See OneSignal's API Reference for the SDK you're using for example code.
    • For testing you can use the 'Player ID' shown on the user page on the dashboard.
      • You can force kill your app and open it again to bring your device to the top of the list.
  2. Send the userId from the getIdsAvailable callback if the pushToken is not null or blank to your server.
  3. Set include_player_ids to the userId on the PostNotification SDK method or on the create notification REST API call.
DevB2F
  • 4,674
  • 4
  • 36
  • 60
Gdeglin
  • 12,432
  • 5
  • 49
  • 65
  • thank you very much. just a small question, OneSignal.sendTag("is_test", "true") - the params are key and value. is the key the deviceid, what is the value for ? – Axil Jun 10 '16 at 02:04
  • @Axil sendTags lets you assign custom key/value pairs to a user. So you could do things like sendTag("age","31") or sendTag("city","San Francisco"), or in this case sendTag("is_test","true"). Next you can target your notifications to users who have or don't have a specific tag and value. – Gdeglin Jun 10 '16 at 03:43
  • thanks. actually, i cant find the part from the server side on how to get the player_id/device id from the tags that were sent by the mobile. – Axil Jun 10 '16 at 05:07
  • To get a user id: let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState() status.subscriptionStatus.userId – Phil Oct 28 '17 at 05:25
  • 1
    One thing i dont understand how can you get somebody elses Player id? Isnt it stored only on his own device? So getidsavailable returns only your own id? – Emil Nov 08 '17 at 10:45