Не удается отправить уведомление из php-скрипта в приложение для Android - PullRequest
1 голос
/ 06 апреля 2019

Ниже приведена структура JSON для отправки push-уведомлений из скрипта PHP в приложение для Android, но я не могу этого добиться.Я использую POSTMAN и получаю код состояния 200, но уведомление не получено.

СТРУКТУРА JSON Я СОБЛЮДАЮ:

https://firebase.google.com/docs/cloud-messaging/concept-options

{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    }
  }
}

PHP SCRIPT

<?php
    $API_KEY="AAAAoyz8W_Q:APA91bFIJxTqoBnQo218QIIXi7uCmHFRP604RTC......";
    $url = "https://fcm.googleapis.com/fcm/send";
    $headers = array(
            'Authorization:key='.$API_KEY,
            'Content-Type:application/json'
    );

    $token="2tJfPjKZQ6UTVG....";
    $notify=array("message"=>
                        array('token' =>$token,
                              'notification'=>array('title'=>"Title",
                              'body'=>"Body")));

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $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($notify));

    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }else{
        echo $ch;
    }
    curl_close ( $ch );

?> 

Ответы [ 2 ]

0 голосов
/ 07 апреля 2019

На самом деле, была проблема с моей переменной $ notifyArray, так как мне нужно передать предопределенные параметры, как определено ниже в ссылке:

https://firebase.google.com/docs/cloud-messaging/http-server-ref

$notify=array('to'=>$token,'notification'=>
                             array('title'=>"Title",body'=>"Subtitle"));
0 голосов
/ 06 апреля 2019

Используйте этот код ниже. Импортируйте этот класс в любой другой файл PHP и просто передайте токен и сообщение fcm.

<?php 
class Firebase {
public function send($registration_ids, $message) {
    $fields = array(
        'registration_ids' => $registration_ids,
        'data' => $message,
    );
    return $this->sendPushNotification($fields);
}

/*
* This function will make the actuall curl request to firebase server
* and then the message is sent 
*/
private function sendPushNotification($fields) {

    //importing the constant files
    require_once 'Config.php';

    //firebase server url to send the curl request
    $url = 'https://fcm.googleapis.com/fcm/send';

    //building headers for the request
    $headers = array(
        'Authorization: key=' . FIREBASE_API_KEY,
        'Content-Type: application/json'
    );

    //Initializing curl to open a connection
    $ch = curl_init();

    //Setting the curl url
    curl_setopt($ch, CURLOPT_URL, $url);

    //setting the method as post
    curl_setopt($ch, CURLOPT_POST, true);

    //adding headers 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    //disabling ssl support
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    //adding the fields in json format 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    //finally executing the curl request 
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }

    //Now close the connection
    curl_close($ch);

    //and return the result 
    return $result;
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...