in this article, we’ll create Push Notification functionality for our app using oneSignal Rest API and Laravel. We’ll add a device and create a push notification using Rest API.
The OneSignal is the fastest and most reliable service to send push notifications, in-app messages, SMS, and emails.
Table of Contents
What’s Push Notification
Web applications use Push Notification as their primary method of communication with their users. We receive a notification consent alert when we visit any website, with the option to approve or reject notifications. Websites develop these alerts to obtain permission to display notifications with the most recent updates, news, and other information. If we give notice display permissions, the notifications will be pushed to website users by the website administrator.
OneSignal Channel Overview
- Web Push Notification
- Mobile Push Notification
- Email Push Notification
- In-app Notification
- SMS
- Rest API
I am using a web push channel for this tutorial.
You can also check out other RESTful API tutorials:
- WordPress Rest api Example and Uses
- How To Consume Post Rest API Using Golang with JSON
- GitHub Rest Api Example and Uses
- How to Access Instagram api Using PHP/Lumen
Free OneSignal Account Features
- Unlimited Mobile Push
- Web Push Up to 10K Subscribers
- Emojis and Images
- Localization
- Real-Time Analytics
- No credit card required
How To Implement Push Notification Using Rest API
In this article, I will show you how to create a simple notification system by using PHP and OneSignal Rest API.
Pre-Requisites
- An Android mobile app, Chrome app or extension, or an Amazon app.
- A Firebase account
- A OneSignal Account, if you do not already have one.
A Firebase Server Key and Firebase Sender ID are required in order to send push notifications to Android mobile app devices.It is optional for Amazon apps.
if you ll use this method instead of OneSignal SDKs, many OneSignal features such as conversion tracking, timezone tracking, language detection, and rich-push won’t work out of the box.
Add a device Using OneSignal Rest API
Register a new device to one of your OneSignal apps
POST https://onesignal.com/api/v1/players
There are two required parameters:
app_id
: Your OneSignal App Iddevice_type
: 0 = iOS, 1 = Android, 2 = Amazon, 3 = WindowsPhone (MPNS) etc.
How To Create OneSignal Rest API Client
var $OneSignalUrl = "https://onesignal.com/api/v1";
private function _client($endpoint) {
$client = new Client([
'base_uri' => $endpoint,
'timeout' => 300,
'headers' => ['Content-Type' => 'application/json', "Accept" => "application/json")
],
'http_errors' => false,
'verify' => false
]);
return $client;
}
How To Add A Device Using OneSignal API
The following method helps to add a new device into OneSignal Notification list.
public function addDevice(Request $request)
{
$response = array();
$parameters = $request->all();
$rules = array(
'app_id' => 'required',
'device_type' => 'required'
);
$messages = array(
'app_id.required' => 'app_id is required.',
'device_type.required' => 'device_type is required.'
);
$validator = \Validator::make($parameters, $rules, $messages);
if(!$validator->fails()) {
$client = $this->_client($this->OneSignalUrl);
try {
$response = $client->post("/players", ['json' => $parameters])->getBody()->getContents();
return json_decode($response);
} catch(Exception $ex) {
Log::info($ex->getMessage());
return 'Unable to create page';
}
return 'Successfully! added device';
} else {
$errors = $validator->errors();
return 'Validation error(s) occurred'.$errors;
}
}
How To Create Notification
Let’s create method that will create OneSignal notification using Rest API.
public function createNotification(Request $request)
{
$response = array();
$parameters = $request->all();
$rules = array(
'app_id' => 'required'
);
$customer_name =
$messages = array(
'app_id.required' => 'app_id is required.'
);
$fields = array(
'app_id' => $parameters['app_id'],
'include_external_user_ids' =>$parameters['user_ids_in_array']),
'channel_for_external_user_ids' => 'push',
'data' => $parameters['data'],
'contents' => array("en" => $parameters['app_id'])
);
$validator = \Validator::make(array('app_id' => $parameters['app_id']), $rules, $messages);
if(!$validator->fails()) {
$client = $this->_client($this->OneSignalUrl);
try {
$response = $client->post("/notifications", ['json' => $parameters])->getBody()->getContents();
return json_decode($response);
} catch(Exception $ex) {
Log::info($ex->getMessage());
return 'Unable to create page';
}
return 'Successfully! added device';
} else {
$errors = $validator->errors();
return 'Validation error(s) occurred'.$errors;
}
}
Really Helpfull for me
it makes my work easier on onesignal
thanks!