Kamalesh kunwar
Kamalesh kunwar

Reputation: 272

How to send push notifications to android mobile from php in react-native?

I am able to send push notifications from the firebase console to mobile in react-native, but when I send from php file the success message is shown but no notification in a mobile. The message in php is :

{"multicast_id":8573*********,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:156161518380026**************"}]}

The user token and server key are correct.In previous projects, I used the similar php file to send notifications developed in android studio which worked perfect. My php code for sending notification:

    <html>
        <head>
            <title>ControlPlus Notification Center</title>
        </head>
        <body>
            <center>
                <br>
                <font size="10" style="bold">ControlPlus Notification Center</font>
                <br> <br> <br> <br> <br>
                <Table class= "b">
                    <tr>
                        <td>
                            <form method = 'POST' action = '?notifyHealth=1'>
                                <div>
                                <input class ='main_button' type = 'submit' value = 'Send Notification'>
                                </div>
                            </form>
                        </td>
                    </tr>

                </Table>
            </center>
        </body>
    </html>



    <?php

        function sendPushNotification() {

            $url = "https://fcm.googleapis.com/fcm/send";

            $serverKey = 'AAAA25************************************Xw';
            $title = "ControlPlus App";
            $body = "New Workorder has been added !! ";
            $notification = array('title' =>$title , 'body' => $body, 'sound' => 'default', 'badge' => '1');
            $arrayToSend = array('to' => 'fL8aUT2un*******************************B2bzLa', 'data' => $notification,'priority'=>'high');
            $json = json_encode($arrayToSend);
            $headers = array();
            $headers[] = 'Content-Type: application/json';
            $headers[] = 'Authorization: key='. $serverKey;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
            curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);

            $response = curl_exec($ch);

            curl_close($ch);

            return $response;

        }

        if(!empty($_GET['notifyHealth'])) { 

            sendPushNotification();

        }
    ?>

Upvotes: 0

Views: 3740

Answers (1)

Patrick Kayongo
Patrick Kayongo

Reputation: 585

There may be an issue in the structure of the JSON payload you are sending. According to the documentation here: https://firebase.google.com/docs/cloud-messaging/concept-options, it should be structured as follows:

{
  "message":{
    "token":"fL8aUT2un*******************************B2bzLa",
    "notification":{
      "title":"ControlPlus App",
      "body":"New Workorder has been added !! "
    }
  }
}

Upvotes: 1

Related Questions