Laravel One Signal is Laravel Wrapper for One Signal. One Signal is a great platform for send a push notification to your users. This package mentions in One Signal's official Document. you can see here
v2.0.0 is released with new features and new APIs with latest version of one signal. this version is supported in laravel 8 and more. if you want to use this package in laravel 7 or lower version than you can use v1.0.0.
- Installation
- Usage
- User Device
- Change Log
- License
Install the package by the following command
composer require ladumor/one-signal:2.0.0
Run the following command to publish config file,
php artisan vendor:publish --provider="Ladumor\OneSignal\OneSignalServiceProvider"
Add the provider to your config/app.php into provider section if using lower version of laravel,
Ladumor\OneSignal\OneSignalServiceProvider::class,
Add the Facade to your config/app.php into aliases section,
'OneSignal' => \Ladumor\OneSignal\OneSignal::class,
Add your api keys and OneSignal app id to your .env,
ONE_SIGNAL_APP_ID=XXXXXX-XXXXXX-XXXXXX-XXXXXX (YOUR APP ID)
ONE_SIGNAL_AUTHORIZE=XXXXXX (REST API KEY)
ONE_SIGNAL_AUTH_KEY=XXXXXXX (YOUR USER AUTH KEY)
You can call them into your code with,
For send push notification, use the sendPush method by calling, you can refer this docs for more fields details.
$fields['include_aliases']['onesignal_id'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy'];
$message = 'hey!! this is test push.!'
OneSignal::sendPush($fields, $message);
Optionally, you can obtain the id of the notification like this,
$notificationID = OneSignal::sendPush($fields, $message);
echo $notificationID["id"];
For send SMS notification, use the sendSMS method by calling,
$fields['include_aliases']['onesignal_id'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy'];
OR
$fields['include_phone_numbers'] = ['+15558675310'];
$fields['sms_from']= "+15558675309";
$message = 'hey!! this is test SMS.!'
OneSignal::sendSMS($fields, $message);
Ensure your Email setup is complete. For send Email notification, use the sendEmail method by calling, see more payload param here
$fields = [
'email_from_name' => 'Shailesh',
'email_from_address' => "[email protected]",
'email_reply_to_address' => "[email protected]",
'email_subject' => "Welcome to Cat Facts",
'email_body' => "html>Welcome to Cat Facts</html>",
'disable_email_click_tracking' => true,
'include_unsubscribed' => true
];
$fields['include_aliases']['onesignal_id'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy'];
OR
$fields['include_email_tokens'] = ['[email protected]'];
OneSignal::sendEmail($fields);
You can customise a contents and pass it in fields. message does not required when you pass contents
$fields['include_aliases']['onesignal_id'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy'];
or
$fields['include_aliases']['external_id'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy'];
$fields['contents'] = array(
"en" => 'English Message',
"es" => 'Spanish Message',
);
OneSignal::sendPush($fields);
For retrieve single notification, use the viewMessage method with id param by calling,
OneSignal::viewMessage($notificationId);
You can check here return response format.
For retrieve all notifications, use the viewMessages method by calling,
OneSignal::viewMessages();
To cancel a notification, use the cancelNotification method by calling,
$notificationID = 'xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy';
OneSignal::cancelNotification($notificationID);
For retrieve notification History, use the getMessageHistory method by calling,
$notificationID = 'xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy';
$params = [
'events' => 'sent', // required
'email' => '[email protected]', // optional, he email address in which to deliver the report.
];
OneSignal::getMessageHistory($notificationID, $params);
This API lets you start a Live Activity by sending a Push Notification., use the startLiveActivity method by calling,
See for more params here
$params = [
'activity_id' => '217aae2b-42ee-4097-bc3f-b7a6e9d15b9b'
];
OneSignal::startLiveActivity($activityType, $params);
Make updates and terminate running Live Activities, use the updateLiveActivity method by calling,
See for more params here
$params = [
'event' => 'update'
];
OneSignal::updateLiveActivity($params);
For register a new user in OneSignal’s system, use the createUser method by calling,
$params = [
'properties' => [
"tags" => [
"foo" => "bar",
"this" => "that"
],
"language" => "en",
"timezone_id" => "America/Los_Angeles",
"lat" => 37.7749,
"long" => -122.4194,
"country" => "US",
"first_active" => 1589788800,
"last_active" => 1589788800,
"purchases" => 0,
"ip" => 3232235777
],
'identity' = [
"external_id" => "An ID, defined by you, to refer to this user in the future",
"facebook_id" => "user_facebook_id",
"amplitude_id" => "user_amplitude_id",
"mixpanel_id" => "user_mixpanel_id",
"custom_alias_N" => "An alternative ID you'll want for retrieving this user's profile data and tags"
],
'subscriptions' => [
[
"type" => "Email",
"token" => "[email protected]",
"enabled" => true,
],
[
"type" => "SMS",
"token" => "phone_number_in_E.164_format",
"enabled" => true,
],
[
"type"=> "iOSPush",
"token"=> "20bdb8fb3bdadc1bef037eefcaeb56ad6e57f3241c99e734062b6ee829271b71",
"enabled"=> true,
"notification_types"=> 1,
"session_time"=> 98,
"session_count"=> 6,
"sdk"=> "",
"device_model"=> "iPhone 14",
"device_os"=> "18.0",
"rooted"=> false,
"test_type"=> 1,
"app_version"=> "5.1.7",
"web_auth"=> "",
"web_p256"=> : ""
]
];
OneSignal::createUser($params);
For retrieve a user including aliases, properties, and subscriptions, use the viewUser method by calling,
OneSignal::viewUser($onesignalId, $aliasId);
For update a user’s properties, tags, and subscriptions, use the updateUser method by calling,
You can refer here for more detail about $params
OneSignal::updateUser($onesignalId, $aliasId, $params);
Delete a user including all associated properties, subscriptions, and identity. use the deleteUser method by calling,
OneSignal::deleteUser($onesignalId, $aliasId);
Retrieve a user’s identity, use the viewUserIdentity method by calling,
OneSignal::viewUserIdentity($onesignalId, $aliasId);
Retrieve a user’s identity by subscription, use the viewUserIdentityBySubscription method by calling,
OneSignal::viewUserIdentityBySubscription($subscriptionId);
Create an alias for a user, use the createAlias method by calling,
Refer docs here for more details
$params = [
'external_id' => "",
'onesignal_id' => "",
];
OneSignal::createAlias($onesignalId, $aliasId, $params);
Create an alias for a user by subscription, use the createAliasBySubscription method by calling,
Refer docs here for
more details
$params = [
'external_id' => "",
'onesignal_id' => "",
];
OneSignal::createAliasBySubscription($subscriptionId, $params);
Delete an alias for a user, use the deleteAlias method by calling,
Refer docs here for more details
OneSignal::deleteAlias($onesignalId, $aliasId, $aliasLabelToDelete);
Add a new subscription to your user. use the createSubscription method by calling,
Refer docs here for more body params
$fields = [
'subscription' => [],
];
OneSignal::createSubscription($fields);
Update an existing subscription, use the updateSubscription method by calling,
Refer docs here for more details
$fields = [
'subscription' => [],
];
OneSignal::updateSubscription($subscriptionId, $fields);
Delete an existing subscription, use the deleteSubscription method by calling,
Refer docs here for more details
OneSignal::deleteSubscription($subscriptionId);
Transfer a subscription to a different user, use the transferSubscription method by calling,
Refer docs here for more details
$fields = [
'identity' => [],
];
OneSignal::transferSubscription($subscriptionId, $fields);
Unsubscribe a user from a notification, use the unsubscribeNotification method by calling,
Refer docs here for more details
OneSignal::unsubscribeNotification($notificationId, $token);
Create a template for push, email and sms, use the createTemplate method by calling,
Refer docs here for push, email and sms template
$fields = [
'name' => "",
'content' => [
"en": "English Message"
],
"sms_from"=>"+1234567890",
"isSMS"=> true,
];
OneSignal::createTemplate($fields);
update a template for push, email and sms, use the updateTemplate method by calling,
Refer docs here for push, email and sms
template
$fields = [
'name' => "",
'content' => [
"en": "English Message"
],
"sms_from"=>"+1234567890",
"isSMS"=> true,
];
OneSignal::updateTemplate($templateId, $fields);
For retrieve a template, use the viewTemplate method by calling,
OneSignal::viewTemplate($templateId);
For retrieve all templates, use the viewTemplates method by calling,
$params = [
'limit' => 10,
'offset' => 0,
'channel' => 'push',
];
OneSignal::viewTemplates($params);
For delete a template, use the deleteTemplate method by calling,
OneSignal::deleteTemplate($templateId);
For transfer a template, use the copyTemplate method by calling,
$fields = [
'target_app_id' => "",
];
OneSignal::copyTemplate($templateId, $fields);
For add a new segment in your application, use the createSegment method by calling,
$fields = [
'name' => 'iOS, Android, Web',
"filters" => array("field" => "device_type", "relation" => "=", "value" => "Android"),
];
return OneSignal::createSegment($fields);
You can check here supported parameters and guide.
OneSignal::deleteSegment('YOUR_SEGMENT_ID')
You can check here for more guide.
Note*: Auth key must be set in one-signal.php how to
get auth_key?
View the details of all of your current OneSignal apps
$apps = OneSignal::getApps();
You can check here api response.
View the details of single of your current OneSignal app or other app by passing app id.
// It's return default site which is configured in config.
$app = OneSignal::getApp();
// you can specify app id as wel but it's optional
$app = OneSignal::getApp('YOUR_APP_ID');
You can check here api response.
Creates a new OneSignal app.
$fields = array(
'name' => "TestByMe"
);
OneSignal::createApp($fields);
You can check here supported parameters and guide.
Update a new OneSignal app.
$fields = array(
'name' => "TestByMe"
);
OneSignal::updateApp($fields);
// you can pass second param as a appId if you want to update other app.. default take from config.
You can check here supported parameters and guide.
View the details of all the outcomes associated with your app.
$fields = array(
'outcome_names' => "os__click.count",
'outcome_time_range' => '1h',
'outcome_platform' => 0,
'outcome_attribution' => 'direct'
);
OneSignal::getOutcomes($fields); // with params
OneSignal::getOutcomes(); // without any params
// you can pass params in this method, it's optional.
You can check here supported parameters and guide.
You can generate a User Device APIs with just one command,
php artisan one-signal.userDevice:publish
this command generate following files,
- UserDeviceAPIController
- UserDeviceAPIRepository
- UserDevice (model)
- Migration
Also, do not forget to add following routes in to the api.php file.
use App\Http\Controllers\API\UserDeviceAPIController;
Route::post('user-device/register', [UserDeviceAPIController::class, 'registerDevice']);
Route::get('user-device/{playerId}/update-status', [UserDeviceAPIController::class, 'updateNotificationStatus']);
Please see Change Log here
The MIT License (MIT). Please see License File for more information




