Sending Firebase push notifications via the PHP form

There is a working code, but I would like to fasten the fill-in form for sending. I tried it, the pushs supposedly go away, but they don't come to the app. I attach the code, I would like to fill in the fields: "type" "mainId" " title" 'message'

If there is someone who will put me on the right path at least by example, I will be very grateful!

<meta charset="utf-8">
<?php
echo "<div style='margin:auto;width:60%;text-align:center;'>";
echo "<hr>";
echo "<h2>Отправка Push уведомления на приложение B2C</h2>";
echo "<hr>";
if (isset($_POST["pushSubmit"])) {
goPushforBtoC();
echo "<h2>PUSH ОТПРАВЛЕН</h2>";
} else {
echo "<form method='post' action='' onsubmit='return confirm(\"Вы точно 
хотите отправить Push уведомление?\");'>";
echo "<input type='submit' name='pushSubmit' value='Отправить Push'>";
echo "</form>";
}
echo "</div>";



function sendNotificationsAndroid($fields) {
$headersAndroid =
    array(
        'Authorization: key=',
        'Content-Type: application/json'
    );

$chAndroid = curl_init();
curl_setopt($chAndroid, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($chAndroid, CURLOPT_POST, true);
curl_setopt($chAndroid, CURLOPT_HTTPHEADER, $headersAndroid);
curl_setopt($chAndroid, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chAndroid, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($chAndroid, CURLOPT_POSTFIELDS, json_encode($fields));
curl_exec($chAndroid);
curl_close($chAndroid);
}


function sendNotificationsIOS($fields) {
$headersIos =
    array(
        'Authorization: key=',
        'Content-Type: application/json'
    );

$chIos = curl_init();
curl_setopt($chIos, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($chIos, CURLOPT_POST, true);
curl_setopt($chIos, CURLOPT_HTTPHEADER, $headersIos);
curl_setopt($chIos, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chIos, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($chIos, CURLOPT_POSTFIELDS, json_encode($fields));
curl_exec($chIos);
curl_close($chIos);
}


function goPushforBtoC() {
 $fieldsAndroid = array(
  'fr' => array(
          "to" => "/topics/testEN",
          "data" => array(
              "title" => 'Trololo',
              'message' => '  Apprendre encore plus!'
          ),
      )
  );
 foreach ($fieldsAndroid as $value) {
  sendNotificationsAndroid($value);
 }



  $fieldsIos = array(
      'en' => array(
          "to" => "/topics/testEN",
          "data" => array(
              "type" => "main"
          ),
          "notification" => array(
              "title" => 'Trololo',
              "body" => '  Learn more!',
              "badge" => 1
          )
      ),
  );
  foreach ($fieldsIos as $value) {
  sendNotificationsIOS($value);
  }

    }

      ?>
Author: Тарас Лейтар, 2018-11-14

1 answers

Here is an example of the working logic for sending push notifications։

<?php
$message = array(
    'title' => 'TITLE',
    'body' => 'MESSAGE',
    'sound' => 'default',
    'badge' => 1
);


$tokens = array( 'APP_TOKEN_HERE' );


$message_status = send_notification( $tokens, $message );




public function send_notification( $tokens, $notification ) {
    header( 'Content-Type: application/json; charset=utf-8' );

    $arrayToSend = array( 'registration_ids' => $tokens, 'notification' => $notification, 'data' => $notification, 'priority' => 'high' );
    $json = json_encode( $arrayToSend );


    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: key=YOUR_KEY';




    $ch = curl_init( "https://fcm.googleapis.com/fcm/send" );
    curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $json );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
    $response = curl_exec( $ch );
    curl_close( $ch );

    $response = json_decode( $response, true );
    return $response;


}

?>
 1
Author: Arsen, 2018-11-14 13:15:12