Php

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.

Related Post

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>
<script>
  window.OneSignal = window.OneSignal || [];
  OneSignal.push(function() {
    OneSignal.init({
      appId: "appid",
    });
  });
</script>

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

OneSignle OnChange Event

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>

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.

View Comments

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

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

3 months ago

What is the Purpose of php_eol in PHP?

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

9 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

9 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

10 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

10 months ago

Categories