Send Notification to specific user using onesignal

2019-04-02 02:04发布

I'm using onesignal for first time in my project. I have to send notification to specific users (wherever user is logged in either on chrome or android device) on some events like whatsapp sends notification on mobile as well as website too and only if user is logged in. I have successfully sent notification to all users. After some R&D on OneSignal, I come to know that I have to send notifications to specific playerId for this purpose. But here are some my questions/doubts.

  • I think player Id is device id. If I'm right, what if current user logged out and another user gets logged in on same device. How can I come to know that I have not to send notification on this player Id as user is changed.
  • Will be there multiple playerIds for single user? One for chrome, one for iOS device, one for Android etc?
  • How I will get playerId of user to store in my database. Because I have to use server rest API to send notifications.

If anyone can explain the points and also describe work flow to store playerid (when and how) in my database, it will be appreciated.

3条回答
The star\"
2楼-- · 2019-04-02 02:37

Just store playerIds into database with login activity. when they logout just fire a query related to remove playerIds from database.

查看更多
姐就是有狂的资本
3楼-- · 2019-04-02 02:47

Player Id is not a device Id, it is a UUID (Unique Universal Identifier) that OneSignal gives

Managing Multiple Users On One Device To manage multiple users that share a device, you will need to detect when User A is on the device and only send them their notifications when they are on the device, otherwise User B will get them.

Method A: Tagging One way to handle this is to tag the user when they sign in to the app, then you can target them by the tag and they will get the notification if the tag is set. Once they log out of the app, you can remove the tag.

So if User B is using the device, then they will get their own tag and will not get User A's notification. So the users will only get the notifications when they are logged into the app.

Method B: CRM or Database The other option is to store on your server the Player IDs of the devices the user is currently logged in with, and then use our API to send notifications using the "include_player_ids" API targeting parameter. for more detail check this One signal player id

查看更多
ら.Afraid
4楼-- · 2019-04-02 02:58

I developed an android app not too long ago which used OneSignal for it's notifications. This was developed in C# however may be useful to you.

I hope these answers help:

-The Player ID is generated by the OneSignal class, and as such need to be retrieved using callback function, such as the code seen below:

void CheckPushRegistration(){
    OneSignal.GetIdsAvailable(IdsAvailable); //Passes the 'callback'

}

private void IdsAvailable(string userID, string pushToken) {
    player_id = userID;
    player_token = pushToken;

    //Make a request to my server to store this information along with device id
    //For easy reference
}

Note: In the code above, I call the function 'CheckPushRegistration' right at the start of the app run. Once it is run it will send the information to the 'IdsAvailable' function. After this I make a quick web request to my server to store the unique player ID, device ID, and token.

-The Player ID, at least in android is based on the devices linked Google Play account if I am not mistaken. Meaning if a user switches accounts it will count/act as a different Player ID (However I could be wrong here)

I assume OneSignal has some way of determining the Player ID or User ID on browsers/other devices. However it is safe to assume they will always be unique and do not carry through based on the user account.

-As mentioned earlier you store it into your database by making a simple web request to your REST API, store only the Device ID, Player ID, and Token information. Perhaps add a check to see if the device ID is not already in the system. I would recommend using the Device ID to identify a user.

You also asked how you would know if a notification was sent correctly, or if it was safe to send. Unfortunately the OneSignal API will only be able to report back on a notification status.

When you do send the notification, you will receive a notification ID back from the server. You can use this at a later stage to checked if a notification was delivered, if it was viewed, and if it was tapped.

I hope this helps to shed some light on the OneSignal system?

查看更多
登录 后发表回答