2017-09-27 4 views
0

tout le monde! J'ai un problème en utilisant PHP pour envoyer une notification FIrebase. Quand je l'envoie à partir de la console Firebase, j'obtiens la notification, mais quand je l'envoie de PHP, je ne reçois aucune notification.Envoi de notification Firebase avec PHP

Avez-vous une idée de quel est le problème?

Voici mon code PHP:

<?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); 


?> 

Je ne reçois pas d'écho à partir du serveur

Répondre

0

essayer cette code.Its fonctionne très bien pour la clé du changement me.just et le jeton

<?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; 
+0

Merci beaucoup! 'icon' où avez-vous stocké myIcon? est le champ 'icône' une icône qui s'affiche lorsque vous faites glisser vers le bas la barre de notification, parce que lorsque je balaye vers le bas je n'ai aucune icône à côté de la notification – Swanson

+0

qui n'est pas nécessaire. et marquer la réponse comme accepté – iCoders

+0

Le développeur android peut personnaliser la notifcation basée sur reuqirement – iCoders

0

J'utilise la fonction suivante pour envoyer une requête HTTP à 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; 
} 

Pour exécuter la fonction que j'utilise le code suivant:

$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"); 

Pour moi, ce code fonctionne sans aucun problème. Assurez-vous d'utiliser le bon jeton de périphérique FCM ($fcm_token) et définissez la clé FCM appropriée ($fcm_key).