skip to Main Content

I have the below code where I am trying to send FCM push notification to an android app targetting specific device. In terminal upon running php push.php I get no result and no errors too.

Please help me what is wrong here, I am able to send using the legacy FCM APIs but I could not using HTTP v1

<?php
$serviceAccount = json_decode(file_get_contents('privatekey.json'), true);

$serverKey = $serviceAccount['private_key'];
$projectId = $serviceAccount['project_id'];

$data = [
    'message' => [
        'token' => 'd6dYpnaLSP2zRk4o6dFfFZ:APA91bG-ZB7XFB-01K54cEVAiyMuqbKyBGhYX-XeiR2QcC3QKv1-MXI4wAy_cwL-d1TLNOUkF-pqt0WQed4b2VcL8OeBxXOPVwrMiWv69TkDRRidOPUw_YjA-ISsgCNoxlWUIb5V_PV6',
        'notification' => [
            'title' => 'Your Title',
            'body' => 'Your Message',
        ],
    ],
];

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

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/v1/projects/notifizy/messages:send');
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($data));

$result = curl_exec($ch);
curl_close($ch);


echo $result;
?>

I have tried all, legacy FCM API works but not this.

2

Answers


  1. // Android app targeting specific device you need RECIVER_TOKEN
    // and get your SERVER API KEY and follow below code

     <?php
        define('SERVER_API_KEY', 'MYSERVERAPIKEY'); 
        $data = array(
           "to" => "$RECIVER_TOKEN",
           "notification" => array(
               "title" => "Test-Title",
               "body" => "Test-Message")
            );
        $headers = array(
            'Content-Type: application/json',
            'Authorization: key=' . SERVER_API_KEY
        );
        $url = 'https://fcm.googleapis.com/fcm/send';
        $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_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        $result = curl_exec($ch);
        if ($result === FALSE) {
            echo 'FCM Send Error: ' . curl_error($ch);
           die('FCM Send Error: ' . curl_error($ch));
        }
        curl_close($ch);
        
        echo $result;
        ?>
    
    Login or Signup to reply.
  2. Simplest way to implement Push notification in PHP

    <?php 
        function send_notification ($tokens, $message_complete)
        {
    
            $url = 'https://fcm.googleapis.com/fcm/send';
            $fields = array(
                'registration_ids' => $tokens,
                'notification' => $message_complete
            );
            $headers = array('Authorization:key=[PUT YOUR KEY HERE]','Content-Type: application/json');
            $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_VERIFYHOST, 0);  
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
            $result = curl_exec($ch);           
            if ($result === FALSE) {
                die('Curl failed: ' . curl_error($ch));
            }
            curl_close($ch);
            return $result;
        }
    
        if($_POST){
            $title = $_POST['title'];
            $message = $_POST['message'];
            $image = $_POST['image'];
            $msg = array
            (
                'title' => $title,
                'body' => $message,
                'image' => $image,
                'url' => 'https://www.google.com',
                'icon' => $image,
                'vibrate' => 1,
                'sound' => 1
            );
            $tokens = array();
            if(isset($_POST['send_test'])) {
                $tokens = $test_token;
                $message_type = "Test";
            } elseif(isset($_POST['send_live'])){
                $tokens[] = $member_token;
                $message_type = "Live";
            }
            $message_complete = $msg;
            $message_status = send_notification($tokens, $message_complete);
            echo "<p class='messageBox'>Push Notification has been sent!</p>" . $message_status;
            echo "<pre>";
            print_r($tokens);
        }else{
            $tokenV = unserialize($_POST['tokensValue']);
        }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search