- Add variables to .env file
ONESIGNAL_APP_ID=
ONESIGNAL_REST_API_KEY=
ONESIGNAL_USER_AUTH_KEY=
- Add OneSignal wrapper for Laravel via composer
composer require jmrieger/onesignal-laravel
- Update config/app.php by adding the following entries.
'providers' => [
// ...
jmrieger\OneSignal\OneSignalServiceProvider::class
];
'aliases' => [
// ...
'OneSignal' => jmrieger\OneSignal\OneSignalFacade::class
];
- Add code to the page or event.
$response = OneSignal::postNotification([
"included_segments" => ["Active Users"],
"contents" => ["en" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean et iaculis enim. Sed egestas porttitor laoreet."],
"headings" => ["en" => "Aliquam consectetur odio sed"],
]);
- Add js file to body
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async='async'></script>
- Add script before close of head to generate laravel vars for use in javascript
<script>
const LaravelVars = {
"onesignal_id": "{{ env("ONESIGNAL_APP_ID") }}",
}
</script>
- Add to JS
var OneSignal = window.OneSignal || [];
OneSignal.push(["init", {
appId: LaravelVars.onesignal_id,
autoRegister: true, /* Set to true to automatically prompt visitors */
httpPermissionRequest: {
enable: true
},
notifyButton: {
enable: true /* Set to false to hide */
}
}]);
- Find out more stuff to add to code at OneSignal API