For the first time I integrated OneSignal Push Notification with PHP
Tags: onesignal, php, pushnotification, tutorial
Published: 2020-05-23 09:15:33

Push notifications are the first thing I see when I switch on my phone at morning. It helps us to get notifications from all apps at a single place and we will not miss them most of the time. 

It was my pleasure to be part of creating such a thing in an application. But it takes some time to do things that we haven't done before and learn the right way as well. 

We needed the Push Notification feature for an app that is being developed in both iOS and Android. My team-mates told me that it is possible to do that using OneSignal Push Notification service. So I tried to integrate using that. I searched Google and asked some of my friends about that. Finally I got a clear idea about how I need to integrate it with the serverside.

First step starts with creating an account in OneSignal and then creating an app in that OneSignal account. From that app we can then send notification using either OneSignal's website interface or the REST API provided by OneSignal. Either way, OneSignal notifications will be delivered to the devices in the same manner. Also, using OneSignal we can send notification to the selected devices too.

OneSignal uses Keys & IDs for integration. For creating the Rest API using PHP we only need to think about those two things. Other side of the integration is done at the client side which can be Android, iOS or Web.

OneSignal provides us PHP code which makes our job pretty easy. The PHP code that OneSignal provides when run will send a curl request to the OneSignal REST API and that will send notifications to all devices or to a single device.

####How we send Push Notification to specific devices?

Here comes the need for a OneSignal Player Id which we get from the client side. OneSignal Player Id is the cool thing we use for sending notification to a particular device. We need to collect the OneSignal Player Id from the devices using a REST Api call to your server.

Then we save the OneSignal Player ID in our database as associated with the loggined user. When we need to send a notification to a user we will select the associated OneSignal Player Id and will assign it to the *'include_player_ids'* parameter and call the Create Notification REST API.

Thus we can send notification to a particular device. If we need to send to multiple devices then all the associated player ids need to be assigned to the include_player_ids.

For this type of notification we need *OneSignal App ID* and *player ids*.

####How to send to all devices?

For sending notification to a particular group of devices subscribed with our OneSignal App we can use the feature called segments, like Active Users, Inactive Users and All etc. So for sending notifications to all users we can use the segment 'All' and that need to be assigned to the parameter called *'included_segments'* and then call the Create Notification REST API.

For this type of notification we need *Onesignal App ID* and *Rest API Key*.

The rest of the PUSH notification integration part needs to be done at the client side. And since I haven't done that, I can only read aloud from the documentation as you also does. Sorry for that.

OneSignal provides Firebase integration and native SDK for Android, JavaScript SDK for Web and also there is native support for iOS too. The Android and iOS devices will receive the Push Notifications and also can get all the notifications previously received as a list after the integration.