Web Push Notification Using OneSignal and PHP

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.

What’s Push Notification

Web applications use Push Notification 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.

OneSignal Channel Overview

  • Web Push Notification
  • Mobile Push Notification
  • Email Push Notification
  • In-app Notification
  • SMS
  • Rest API

I am using web push channel for this tutorial.

Free OneSignal Account Features

  • Unlimited Mobile Push
  • Web Push Up to 10K Subscribers
  • No credit card required
  • Unlimited Segmentation
  • Delivery Scheduling
  • Emojis and Images
  • Localization
  • A/B Testing
  • Real-Time Analytics

How To Implement Web Push Notification

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 website using Official Documentation.

There are following option available for web push notification:


Step 1: Download JavaScript SDK

Let’s Download OneSignal SDK files.You can also download the files here.

Upload SDK

Unzip the OneSignal SDK files. There should be two files:

  • OneSignalSDKWorker.js
  • OneSignalSDKUpdaterWorker.js

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.

Option 1: Configuration Code Using Typical Site Option

You will need to paste the provided code into your website’s head index.php file or entry file of your project.

Option 2: Configuration Code Using Custom Code

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, as well as any other logic you want to utilise.

OneSignle OnChange Event

We can also determine whether or not a user has subscribed to notifications using the js event handler

How To Check Web Push Notification

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 record.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.