Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Firebase notification with PHP

everyone! I am having a problem using PHP to send FIrebase notification. When I send it from Firebase console, I get the notification, but when I send it from PHP, I don't receive any notification.

DO you have any idea what is the problem?

Here is my PHP code:

<?php
$message = 'ojlaasdasdasd';
$title = 'ojla';
$path_to_fcm = 'https://fcm.googleapis.com/fcm/send';    $server_key='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5LnDZO2BpC2aoPMshfKwRbJAJfZL8C33qRxxxxxxxxxxxxL6';
$key = 'eqnlxIQ1SWA:APA91bGf1COAZamVzT4onl66_lEdE1nWfY7rIADcnt9gtNYnw7iWTwa7AYPYTHESFholkZ89ydCQS3QeL-lCIuiWTXiqoDREO0xhNdEYboPvqg8QsBYkrQVRlrCLewC4N-hHUja1NG4f';
$headers = array(
                'Authorization:key=' .$server_key,
                'Content-Type: application/json');

$fields = array
            (
                'to'        => $key,
                'notification'  => array('title' => $title,'body' => $message)
            );


$payload = json_encode($fields);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, '$path_to_fcm' );
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_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt( $ch,CURLOPT_POSTFIELDS, $payload);
$result = curl_exec($ch);
echo $result;
curl_close($ch);


?>

I don't get any echo from the server

like image 421
Swanson Avatar asked Sep 27 '17 18:09

Swanson


2 Answers

try this code.Its working fine for me.just change key and token

<?php
define('API_ACCESS_KEY','Api key from Fcm add here');
 $fcmUrl = 'https://fcm.googleapis.com/fcm/send';
 $token='235zgagasd634sdgds46436';

     $notification = [
            'title' =>'title',
            'body' => 'body of message.',
            'icon' =>'myIcon', 
            'sound' => 'mySound'
        ];
        $extraNotificationData = ["message" => $notification,"moredata" =>'dd'];

        $fcmNotification = [
            //'registration_ids' => $tokenList, //multple token array
            'to'        => $token, //single token
            'notification' => $notification,
            'data' => $extraNotificationData
        ];

        $headers = [
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        ];


        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$fcmUrl);
        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($fcmNotification));
        $result = curl_exec($ch);
        curl_close($ch);


        echo $result;
like image 137
Vision Coderz Avatar answered Sep 24 '22 18:09

Vision Coderz


I use the following function to send a HTTP Request to Firebase:

function request($url, $body, $method = "POST", $header = "Content-type: application/x-www-form-urlencoded\r\n")
{
    switch ($method) {
        case 'POST':
        case 'GET':
        case 'PUT':
        case 'DELETE':
            break;
        default:
            $method = 'POST';
            break;
    }
    $options = array(
        'http' => array(
            'header' => "$header",
            'method' => "$method",
            'content' => $body
        )
    );
    $context = stream_context_create($options);
    $data = file_get_contents($url, false, $context);
    $data = json_decode($data, true);
    return $data;
}

To execute the function I use the following code:

$fcm_key = "";
$body = array("data" => $data, "to" => "$fcm_token");

$body = json_encode($body);
$json = request("https://fcm.googleapis.com/fcm/send", $body, "POST", "Authorization: key=$fcm_key\r\nContent-Type:application/json");

For me, this code works without any problems. Be sure that you use the right FCM device token ($fcm_token) and set the right FCM key ($fcm_key).

like image 39
Julian Schmuckli Avatar answered Sep 24 '22 18:09

Julian Schmuckli