Google trends Public Holidays Coupon Code Code Compiler

Send Push Notification in Android using Firebase and PHP


Jun 9, 2019

In this tutorial, I will show you how to implement send push notifications from Firebase or from Custom PHP Console. This tutorial will help you, that lets you deliver messages at no cost. It can be used to send an unlimited number of notifications to your application.

Here is the php function for send push notification to android device.


$message = 'Hi friends!';
$deviceToken = 'ENTER-HERE-ANDROID-DEVICE-TOKEN';
send_push_to_android($message, $deviceToken);

function send_push_to_android($message, $deviceToken) {

    $msg = array
        (
        'body' => $message,
        'message' => $message,
        'title' => 'Test Notification',
        'group_name' => 'hi3',
        'number' => 'hi4',
        'type' => 'push',
        'icon' => 'myicon', /* Default Icon */
        'sound' => 'mySound'/* Default sound */
    );

	#API access key from Google API's Console
	#prep the bundle

    $fields = array
        (
        'to' => $deviceToken,
        'notification' => $msg
    );


    $headers = array
        (
        'Authorization: key=FIRE_BASE_KEY', // Used valid firebase key here
        'Content-Type: application/json'
    );
	
	#Send Reponse To FireBase Server
    $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, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);
    curl_close($ch);
	
	#Echo Result Of FireBase Server
    $data = json_decode($result, TRUE);

    if ($data['success'] == 1) {
        return TRUE;
    } else {
        return FALSE;
    }
}


                

Copyright 2024. All rights are reserved