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 a Push Notification

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.

OneSignal Channel Overview

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

I am using a web push channel for this tutorial.

Free OneSignal Account Features

  • Unlimited Mobile Push
  • Web Push Up to 10K Subscribers
  • No credit card is 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 the website using Official Documentation.

There are the following options available for web push notifications:


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.

<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>
  window.OneSignal = window.OneSignal || [];
  OneSignal.push(function() {
      appId: "appid",

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 and any other logic you want to utilize.

<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>
  window.OneSignal = window.OneSignal || [];
  OneSignal.push(function() {
      appId: "xxxx",
      safari_web_id: "web.onesignal.auto.xxxx",
      notifyButton: {
        enable: true,
      subdomainName: "phpflow",

OneSignle OnChange Event

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

OneSignal.push(function() {
  OneSignal.on('subscriptionChange', function(isSubscribed) {
    if (isSubscribed) {
      console.log('Thanks! for subscription');
        console.log("sorry dear, unsubscribed");

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

2 thoughts on “Web Push Notification Using OneSignal and PHP

Leave a Reply

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