Skip to content

Instantly share code, notes, and snippets.

@blueridgemountains1
Last active August 14, 2017 16:24
Show Gist options
  • Save blueridgemountains1/5b1b78519b9542e8dca2ca0ea17ee4ca to your computer and use it in GitHub Desktop.
Save blueridgemountains1/5b1b78519b9542e8dca2ca0ea17ee4ca to your computer and use it in GitHub Desktop.
Add OneSignal to Laravel
  1. Add variables to .env file
ONESIGNAL_APP_ID=
ONESIGNAL_REST_API_KEY=
ONESIGNAL_USER_AUTH_KEY=
  1. Add OneSignal wrapper for Laravel via composer
composer require jmrieger/onesignal-laravel
  1. Update config/app.php by adding the following entries.
'providers' => [
	// ...
	jmrieger\OneSignal\OneSignalServiceProvider::class
];

'aliases' => [
   	// ...
   	'OneSignal' => jmrieger\OneSignal\OneSignalFacade::class
   ];
  1. 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"],
]);
  1. Add js file to body
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async='async'></script>
  1. Add script before close of head to generate laravel vars for use in javascript
<script>
const LaravelVars = {
	"onesignal_id": "{{ env("ONESIGNAL_APP_ID") }}",
}
</script>
  1. 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 */
	}
}]);
  1. Find out more stuff to add to code at OneSignal API
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment