Skip to content
Advertisement

OneSignal push notification with PHP

I’m using onesignal first time. I have already spent too much time on R&D. But output is almost zero. So here I go with my requirements.

  • I have a website. iOS app of the site and Android app too. Just like whatsapp or facebook.
  • When user logged in any of the above, user should get webpush notification or iOS notification, based on logged in device.
  • If user is logged in website as well as iOS app at a time, user should get notification on both.
  • If user gets logged out from iOS, user should not get notification in iOS device till he login again.
  • If another user login in to same iOS app, he should not get notifications of last logged in user. He should get only notifications related to him.
  • Same for website too.

I have successfully developed onesignal demo. But it is user independent. If I trigger notification, all the device gets notification. So here are my questions.

  • I have worked on GCM (I’m from PHP background). So as per my knowledge, push notification works on device id. So should I store multiple device id for a single user in my mysql database (for chrome, iOS, android etc)?
  • How will I get that device id? Remember, if user is not logged in, I don’t want to send notification.
  • How to store it in my Database? I will pass that device id to my server by calling API and my server will store it in database. But when I should send? Because If user is not logged in, then how can I come to know that for which user I have to save device id?
  • How to check if user is changed on device or is same or is logged out?

If anyone can show me basic example, it will be appreciated. I’m from PHP side. It will be good for me if I get answer from PHP developer’s view. I have already asked this question on stackoverflow. But answer was from Android view. But I think he doesn’t know that on server side, we have to manage all the devices. So I haven’t get much more from that answer.

Advertisement

Answer

Onesignal is a good platform to send push notifications to different types of devices. I’m using the same in my project. What I’m doing is that, whenever the user is logged in, I’ll get the device token from the client and create a user in onesignal using their API. (I believe you know how to configure the onesignal with google project number & google API key for android, p12 files for ios, etc…)

Android Perspective:

  • We have to keep one thing in our mind that, a device’s gcm token will always remains the same, as long as the google project number and google API key is not changed.
  • If the same project is having a different application like b2b, b2c, etc… (with same DB and backend), your google API key will change, so is the gcm token and so is the onesignal player id.

So, for the same google API key, onesignal will always return the same player id, even though you delete and create the user again. In a big project, developers should expect that a user might be logged in to different devices (android, ios, etc). So a user can have multiple tokens, and so is the player id. One more requirement which you’ve is that the user doesn’t want to get notified if he’s not logged in.

So keeping all of your requirements in mind, I can suggest you one MySQL table structure to store the user details.

  1. One table to store the device type (optional)

      -id
      -device_type
    
  2. One to store the tokens and player ids

      -token_id
      -user_id
      -device_id
      -device_token
      -onesignal_player_id
      -subscription
    

These two tables are needed for onesignal to work.

Case 1:

Whenever a user try for logging in, first check the token table. Find if there is any other user having the same given token (which means another user logged in with the same device). Delete such token if there is any. Then create a new player in onesignal and save the details into the token table. Mark subscription as 1, which is subscribed for notification. Whenever the user logs out, mark the subscription to 0, which is unsubscribed for notification.

Case 2:

Whenever the user logs in, check for the token whether it is already present in the table. If it is yes and the owner of the token is the same, then just mark the subscription as 1. So that further notifications will be received by him.

Case 3:

When a user uses another device to login (multiple devices at a time), the gcm token will be different for sure. So save that as another token for the same user and mark the subscription as 1. So, both devices will get the notification.

In the end, before sending the notifications, fetch all tokens of that particular user which is subscribed and send the notification to all at a single shot. That will fix your problem.

I know this might be a bit confusing. But don’t worry, we can fix it.

enter image description here

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement