Send Android Push Notifications in PHP

Hello friends,

In previous blog post, we told you about Sending Push Notifications in IOS. Today we have bought up the similar topic but for the Android. How you can send Android Push Notifications in PHP.

So mates, let us start.

As we know push notifications are the becoming the base of any application since educate and keep your customers aware about your products events are the main interest of a company. If Your Android App has PHP as an API language then you should create a customized panel for the user where he/she can trigger the unlimited notifications to its users.

Like Apple push notifications runs through the certificate file generated by the IOS Developer in Android the process is much easier and less vulnerable to errors.

So let us start with the steps.

  1. First and foremost important step is to create the Firebase account and get the API key which will be used to send the notifications. If you do not know how to create account simple ask your Android developer.
  2. Now second step is to obtain the Device ID of the user whom you want to send the push notifications. Remember the Device ID should be stored in the database to uniquely identify the user’s device.
  3. Now we have to perform the API Integration were we will hit the FCM server and send the message along with Device ID verified by the API key of your FCM account

So Let us start the API Integration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php

$fields = array (
                'registration_ids' =>  "DEVICE ID OF USER YOU WANT TO SEND PUSH",
                'data'=> array( "message" =>'Hello This is test notification'),
                );


$apiKey="PASTE YOUR API KEY HERE";  //FCM API KEY
   
      $url = 'https://fcm.googleapis.com/fcm/send';   //URL to be hit

 $headers = array (
            'Authorization: key=' . $apiKey,
            'Content-Type: application/json'
    );                                             //SET the Headers

       $ch = curl_init();
        // Set the url, number of POST vars, POST data
        curl_setopt( $ch, CURLOPT_URL, $url);   //Set The URL
        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( $fields ) );   //encode dat in json format
       
        // Execute post
        $result = curl_exec($ch);
             print_r($result);      //Print the results you can use your comparisons and message.
        // Close connection
       
        curl_close($ch);

?>

So the above code will let you send the Push Notifications to the users you intend to send we can also send PUSH to multiple users at a single click of a button.

So that’s how easy it is to create your own panel for sending push notifications to the users.

We Hope this tutorial helps our young developers.

You can download the source code here.

Because we believe hurdles aren’t really hurdles.
Cheers!

Be First to Comment

Leave a Reply

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