norkunas / onesignal-php-api

OneSignal API for PHP
MIT License
234 stars 83 forks source link

How to get current device id? #73

Closed faraz9x closed 5 years ago

faraz9x commented 7 years ago

Please guide, How to get current device or player id.

norkunas commented 7 years ago

So fetch the devices list and take the ID: $devices = $api->devices->getAll();

faraz9x commented 7 years ago

how i know which is the id of current device

norkunas commented 7 years ago

It's impossible because in backend there is no "current device"

faraz9x commented 7 years ago

okay, i meant any how whether in backend or front end, we can get current device / player id for onesignal in javascript: OneSignal.push(["getUserId", function(useridi) { alert("USERID:"+ useridi); }]);

norkunas commented 7 years ago

I would suggest you to set the user email https://documentation.onesignal.com/docs/web-push-sdk#section--synchashedemail- to onesignal and then in backend target the users by email

norkunas commented 7 years ago

Or just assign the device ID to the backend user

Simbaclaws commented 6 years ago

Is it not a posibility to create a backend REST API with something like a $_POST request to the server in order to send the user's player id or device id to the server, Save that in your backend and then push it to that specific device whenever needed? This scenario is in case the user wants to create push notifications with specific dates from within their frontend. Not sure if that's what faraz9x wanted, but definitly something I want to have.

EDIT: I realize now that the onesignal SDK has such functionality and there is no need to do this on the backend... My mistake.

norkunas commented 6 years ago

:) also this is business logic and not the library responsibility.

eqxDev commented 6 years ago

@faraz9x @Simbaclaws

Hi,

I am starting to do this I was wondering how you got this to work in the end?

How can I link a User to a list of devices?

norkunas commented 6 years ago

@eqxDev pass the user information to your frontend from backend somehow and then add tags:

OneSignal.sendTag('user_id', this.user.id);
OneSignal.sendTag('real_name', this.user.fullName);
OneSignal.syncHashedEmail(this.user.email);

then you'll be able to target users by these tags from backend, I am using like this in my cordova application

eqxDev commented 6 years ago

Thanks @norkunas what will I use the tags for? Can I set them to anything?

Also does one signal has its own user-agent I can detect?

norkunas commented 6 years ago

Tags are good for direct targeting, for example let's say if you are sending some message to the user in your system and if user subscribed to get push notifications, then you can target onesignal notification by the user ID and he will receive notification that you sent message for him.

Tags can be anything but must be in a String format.

About the last question, could you explain further?

farazhere commented 6 years ago

@eqxDev yeah onesignal will create a unique device id for all registered devices but the fact that same user can use the application from multiple devices makes us use tags

farazhere commented 6 years ago

and tags can be set from both front end (JS SDK) and from backend (REST API).

eqxDev commented 6 years ago

Using JavaScript how would I get the uuid of device so I can use AJAX to send it to the database?

norkunas commented 6 years ago

@eqxDev https://stackoverflow.com/a/42923420/1815160 try this :)