ladumor / one-signal
Laravel Wrapper for OneSignal.
Fund package maintenance!
www.buymeacoffee.com/ladumor
Installs: 235 291
Dependents: 0
Suggesters: 0
Security: 0
Stars: 119
Watchers: 2
Forks: 21
Open Issues: 0
Requires
- php: >=5.4.0
- ext-curl: *
- ext-json: *
- illuminate/support: ~5.0|^6.0|^7.0
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~5.0
README
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
Give a Star if this package realy usefull to you. it's free 😆
🎞️ here are video tutorials
How to install and how to implement notifications and devices APIs.
how to implement Segment and Apps APIs.
Requirements.
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.
Contents
- Installation
- Usage
- User Device
- Change Log
- License
Watch Other Lavavel tutorial here
Installation
Install the package by the following command
composer require ladumor/one-signal:2.0.0
Publish the config file
Run the following command to publish config file,
php artisan vendor:publish --provider="Ladumor\OneSignal\OneSignalServiceProvider"
Add Provider
Add the provider to your config/app.php
into provider
section if using lower version of laravel,
Ladumor\OneSignal\OneSignalServiceProvider::class,
Add Facade
Add the Facade to your config/app.php
into aliases
section,
'OneSignal' => \Ladumor\OneSignal\OneSignal::class,
Add ENV data
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,
Usage
Send Notification
Push
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"];
SMS
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' => "sml@gmail.com",
'email_reply_to_address' => "reply@gmail.com",
'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'] = ['sample@gmail.com'];
OneSignal::sendEmail($fields);
Customise Contents
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);
Get Single Notification
For retrieve single notification, use the viewMessage
method with id param by calling,
OneSignal::viewMessage($notificationId);
You can check here return response format.
Get All Notifications
For retrieve all notifications, use the viewMessages
method by calling,
OneSignal::viewMessages();
Cancel Notification
To cancel a notification, use the cancelNotification method by calling,
$notificationID = 'xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy';
OneSignal::cancelNotification($notificationID);
Notification History
For retrieve notification History, use the getMessageHistory
method by calling,
$notificationID = 'xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy';
$params = [
'events' => 'sent', // required
'email' => 'sample@gmail.com', // optional, he email address in which to deliver the report.
];
OneSignal::getMessageHistory($notificationID, $params);
Start Live Activity
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);
Update Live Activity
Make updates and terminate running Live Activities, use the updateLiveActivity
method by calling,
See for more params here
$params = [
'event' => 'update'
];
OneSignal::updateLiveActivity($params);
Create User
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" => "sample@email.com",
"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);
View User
For retrieve a user including aliases, properties, and subscriptions, use the viewUser
method by calling,
OneSignal::viewUser($onesignalId, $aliasId);
Update User
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 User
Delete a user including all associated properties, subscriptions, and identity. use the deleteUser
method by calling,
OneSignal::deleteUser($onesignalId, $aliasId);
View User Identity
Retrieve a user’s identity, use the viewUserIdentity
method by calling,
OneSignal::viewUserIdentity($onesignalId, $aliasId);
View User Identity By Sponsorship
Retrieve a user’s identity by subscription, use the viewUserIdentityBySubscription
method by calling,
OneSignal::viewUserIdentityBySubscription($subscriptionId);
Create Alias
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 Alias by subscription
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 Alias
Delete an alias for a user, use the deleteAlias
method by calling,
Refer docs here for more details
OneSignal::deleteAlias($onesignalId, $aliasId, $aliasLabelToDelete);
Create Subscription
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 Subscription
Update an existing subscription, use the updateSubscription
method by calling,
Refer docs here for more details
$fields = [
'subscription' => [],
];
OneSignal::updateSubscription($subscriptionId, $fields);
Delete Subscription
Delete an existing subscription, use the deleteSubscription
method by calling,
Refer docs here for more details
OneSignal::deleteSubscription($subscriptionId);
Transfer Subscription
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 Notification
Unsubscribe a user from a notification, use the unsubscribeNotification
method by calling,
Refer docs here for more details
OneSignal::unsubscribeNotification($notificationId, $token);
Create Template
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 Template
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);
View Template
For retrieve a template, use the viewTemplate
method by calling,
OneSignal::viewTemplate($templateId);
View Templates
For retrieve all templates, use the viewTemplates
method by calling,
$params = [
'limit' => 10,
'offset' => 0,
'channel' => 'push',
];
OneSignal::viewTemplates($params);
Delete Template
For delete a template, use the deleteTemplate
method by calling,
OneSignal::deleteTemplate($templateId);
Copy Template
For transfer a template, use the copyTemplate
method by calling,
$fields = [
'target_app_id' => "",
];
OneSignal::copyTemplate($templateId, $fields);
Create Segment
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')
Delete Segment
You can check here for more guide.
Apps
Note*: Auth key must be set in one-signal.php
how to
get auth_key?
View Apps
View the details of all of your current OneSignal apps
$apps = OneSignal::getApps();
You can check here api response.
View App
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.
Create App
Creates a new OneSignal app.
$fields = array(
'name' => "TestByMe"
);
OneSignal::createApp($fields);
You can check here supported parameters and guide.
Update App
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 Outcomes
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.
User Device
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']);
Change Log
Please see Change Log here
License
The MIT License (MIT). Please see License File for more information