This tutorial help to create Push Notification with PHP via Onesignal. I am using PHP 8, MySQL and one signal Rest API. The OneSignal is the fastest and most reliable service to send push notifications, in-app messages, SMS, and emails.
Web applications use Push notifications 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 the notice to display permissions, the notifications will be pushed to website users by the website administrator.
I am using a web push channel for this tutorial.
In this article, I will show you how to create a simple notification system by using PHP.
I am assuming you have set up your website into the onesignal dashboard, if not please setup the website using Official Documentation.
There are the following options available for web push notifications:
Let’s Download OneSignal SDK files. You can also download the files here.
Unzip the OneSignal SDK files. There should be two files:
The OneSignal SDK files must be publicly accessible and can be placed at the top-level root of your site. You need to upload SDK at the root of your web files.
You will need to paste the provided code into your website’s head index.php
file or entry file of your project.
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script> <script> window.OneSignal = window.OneSignal || []; OneSignal.push(function() { OneSignal.init({ appId: "appid", }); }); </script>
This code must be placed in the head section of all pages on your site where users can subscribe.
Because you chose Custom Code, you’ll need to add more code to this part to prompt customers to subscribe and any other logic you want to utilize.
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script> <script> window.OneSignal = window.OneSignal || []; OneSignal.push(function() { OneSignal.init({ appId: "xxxx", safari_web_id: "web.onesignal.auto.xxxx", notifyButton: { enable: true, }, subdomainName: "phpflow", }); }); </script>
We can also determine whether or not a user has subscribed to notifications using the js event handler.
<script> OneSignal.push(function() { OneSignal.on('subscriptionChange', function(isSubscribed) { if (isSubscribed) { console.log('Thanks! for subscription'); }else{ console.log("sorry dear, unsubscribed"); } }) }); </script>
Visit your website and, depending on the prompt settings you specified, you should be requested to subscribe to push notifications.
You can check your OneSignal Dashboard through Audience > All Users. You can see all device records.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
How to get user id for future personal notifications?
you need to store details in db and use that that details for future notification