Как отправить push-уведомления на мобильный Android от php в реагировать на родной? - PullRequest
0 голосов
/ 27 июня 2019

Я могу отправлять push-уведомления с консоли Firebase на мобильное устройство в реактивном режиме, но когда я отправляю из php-файла, отображается сообщение об успешном завершении, но на мобильном телефоне нет уведомлений. Сообщение в php:

{ "multicast_id": 8573 *********, "успех": 1, "неудача": 0, "canonical_ids": 0, "результаты": [{ "message_id": "0: 156161518380026 ************** "}]}

Токен пользователя и ключ сервера верны. В предыдущих проектах я использовал аналогичный php-файл для отправки уведомлений, разработанных в android studio, которые отлично работали. Мой php код для отправки уведомления:

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

        }
    ?>

1 Ответ

1 голос
/ 27 июня 2019

Может быть проблема в структуре полезной нагрузки JSON, которую вы отправляете.Согласно документации здесь: https://firebase.google.com/docs/cloud-messaging/concept-options, она должна быть структурирована следующим образом:

{
  "message":{
    "token":"fL8aUT2un*******************************B2bzLa",
    "notification":{
      "title":"ControlPlus App",
      "body":"New Workorder has been added !! "
    }
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...