Firebase Cloud Messaging - Failed to send notification via server

Asked

Viewed 35 times

0

I have an application that sends messages from Server to Android through Firebase and is working. However, I am developing a new project with "andoirdx" and after the new changes in Firebase I am no longer able to use the same application of the PHP server as the basis to send notifications. Using the Firebase console I can send notifications to my application. Follow the PHP server code I’m using as a base.

Send.php

<?php
// Enabling error reporting
error_reporting(-1);
ini_set('display_errors', 'On');

require_once __DIR__ . '/firebase.php';
require_once __DIR__ . '/push.php';

$firebase = new Firebase();
$push = new Push();

$message = isset($_GET['message']) ? $_GET['message'] : '';
$tipo = isset($_GET['tipo']) ? $_GET['tipo'] : '';
$latitude = isset($_GET['latitude']) ? $_GET['latitude'] : '';
$longitude = isset($_GET['longitude']) ? $_GET['longitude'] : '';
$push_type = isset($_GET['push_type']) ? $_GET['push_type'] : '';

$push->setMessage($message);
$push->setTipo($tipo);
$push->setLatitude($latitude);
$push->setLongitude($longitude);
$push->setIsBackground(false);

$json = '';
$response = '';

if ($push_type == 'global') {
    $json = $push->getPush();
    $response = $firebase->sendToTopic('global', $json);
} 
else if ($push_type == 'individual') {
    $json = $push->getPush();
    $regId = isset($_GET['token']) ? $_GET['token'] : '';
    $response = $firebase->send($regId, $json);
}

echo "Request: ".json_encode($json)."<p>";
echo " - Response: ". json_encode($response)."<p>";
?>

php push.

<?php
class Push { 
    private $message;
    private $tipo;
    private $latitude;
    private $longitude;
        
    /* flag indicating whether to show the push notification or not this flag will be useful 
     * when perform some opertation in background when push is recevied     
    */
    private $is_background;

    function __construct() {  }
    

    public function setMessage($message) {
        $this->message = $message;
    }

    public function setIsBackground($is_background) {
        $this->is_background = $is_background;
    }
    
    public function setTipo($tipo) {
        $this->tipo = $tipo;
    }
        
    /**
     * @param field_type $latitude
     */
    public function setLatitude($latitude) {
        $this->latitude = $latitude;
    }

    /**
     * @param field_type $longitude
     */
    public function setLongitude($longitude) {
        $this->longitude = $longitude;
    }

    public function getPush() {
        $res = array();       
        $res['data']['message'] = $this->message;
        $res['data']['tipo'] = $this->tipo;
        $res['data']['latitude'] = $this->latitude;
        $res['data']['longitude'] = $this->longitude;
        
        return $res;
    }
}

firebase.php

<?php
class Firebase {

    // sending push message to single user by firebase reg id
    public function send($to, $message) {
        $fields = array(
            'to' => $to,
            'data' => $message,
        );
        return $this->sendPushNotification($fields);
    }

    // Sending message to a topic by topic name
    public function sendToTopic($to, $message) {
        $fields = array(
            'to' =>'/topics/'. $to,
            'data' => $message,
        );
        return $this->sendPushNotification($fields);
    }

    // sending push message to multiple users by firebase registration ids
    public function sendMultiple($registration_ids, $message) {
        $fields = array(
            'to' => $registration_ids,
            'data' => $message,
        );

        return $this->sendPushNotification($fields);
    }

    // function makes curl request to firebase servers
    private function sendPushNotification($fields) {
        
        require_once __DIR__ . '/config.php';

        // Set POST variables
        $url = 'https://fcm.googleapis.com/fcm/send';

        $headers = array(
            'Authorization: key=' . FIREBASE_API_KEY,
            'Content-Type: application/json'
        );
        // Open connection
        $ch = curl_init();

        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }

        // Close connection
        curl_close($ch);
        return $result;
    }
}

?>

config.php

<?php

// Firebase API Key
define('FIREBASE_API_KEY', 'XXXXXY-xxxx:XXXXXXXxxxxxxxxxxxXXXXXXXXXXXXXXXXXXxxXXXX_XXXXXXXXXXXXXX-0000000-XxX0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
?>

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.