Firebase Notification Send Example

  • 29-04-2022
  • 2118
  • PHP
  • Haresh Chauhan

What is push notification ?

Push notification is real time pushing the message to web application in browser or mobile application for real time communication or interaction with the user, Nowadays push notification mostly use for marketing purpose and live broadcasting or others like offer regarding information or other information regarding to their customer from the companies.

What is Firebase notification (FCM) ?

Firebase developed by google for creating mobile application and web application, the Firebase platform is depending on json data store, It NOSQL database programming which is store the data in to json object.

Here below is the working example of a Firebase which is sending the push notification to web application using PHP CURL send request to Firebase cloud server.

IOS Push Notification Send PHP Example

Firebase Cloud Message With PHP CURL

<?php

    $notification = [
        'notification' => [
            'title' => "You notification title put here",
            'body' => "You message put here",
            'image' => "Image url if you want to show else null",
        ],
        'priority' =>  "high",
        'data' => [
            'icon' => 'Put your icon url here.',
            'sound' => 'If Yes then 1 or no for 2',
            'vibrate' => 'If Yes then 1 or no for 2',
            'click_action' => 'NOTIFY_CLICK',
            'notification_type' => 'put your notification type (optional)',
            'info' => [
                'title' => "You notification title put here",
                'body' => "You message put here",
                'image' => "Image url if you want to show else null",
            ]
        ],
        'to' => 'device token', // FOR SINGLE TOKEN ONLY
        'registration_ids' => ['tokne 1','tokne 2'] // MULTIPLE TOKEN FOR MULTIPLE NOTIFICATION BLUK
    ];

    $headers = [
        'Authorization: key=<YOUR-FIREBASE-AUTH-KEY>',
        'Content-Type: application/json'
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,'https://fcm.googleapis.com/fcm/send');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($notification));
    $result = curl_exec($ch);
    curl_close($ch);
        
    echo 'Notification successfully';

Thank you for read post.


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :