0

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();

        }
    ?>
4
  • how about react-native side? what library/package you used to implement push-notifications inside RN? Commented Jun 27, 2019 at 6:44
  • I have installed react-native-firebase package and added all the dependencies to be added. I followed: medium.com/@anum.amin/… Commented Jun 27, 2019 at 6:53
  • very good then. Your messages most probably do not fall under "Notification-only messages" category and you should handle them yourself. Do you implement these " this.showAlert(title, body);" lines too? maybe add RN code too to description Commented Jun 27, 2019 at 7:04
  • Where should I implement those methods ? Could you please guide me through this, I already worked too long on this. Commented Jun 27, 2019 at 7:16

1 Answer 1

1

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 !! "
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I used the 'data' instead of 'notification'. But 'data' worked on my previous projects on android studio. Thanks a lot for you help. Finally solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.