web-dev-qa-db-fra.com

comment envoyer des notifications Push sur un iPhone en utilisant fcm (Firebase Console) en PHP?

Lors de l'envoi de la notification à partir de la console Firebase, Notification fonctionne correctement.

 firebase console

Je reçois des notifications Push sur un appareil iOS.

Voici le code que j'utilise pour envoyer des notifications Push à un iphone en php en utilisant FCM ..

<?php  $ch = curl_init("https://fcm.googleapis.com/fcm/send");

    //The device token.
    $token = "";

    //Title of the Notification.
    $title = "Carbon";

    //Body of the Notification.
    $body = "Bear island knows no king but the king in the north, whose name is stark.";

    //Creating the notification array.
    $notification = array('title' =>$title , 'text' => $body);

    //This array contains, the token and the notification. The 'to' attribute stores the token.
    $arrayToSend = array('to' => $token, 'notification' => $notification);
    //Generating JSON encoded string form the above array.
    $json = json_encode($arrayToSend);

    //Setup headers:
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: key= abcdgfdk'; //server key here

    //Setup curl, add headers and post parameters.
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);       

    //Send the request
    $response = curl_exec($ch);

    //Close request
    curl_close($ch);
    return $response; ?>

Et cela renvoie la réponse suivante:

{"multicast_id":7847791275395796141,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1473926169782959%51b989d251b989d2"}]}

S'il vous plaît me suggérer ce que je fais mal? J'utilise le même code pour Android aussi avec sa clé de serveur et son jeton de périphérique et cela fonctionne bien ...

5
Simerjit Parmar

Merci shubank .. ta réponse fonctionne ... La seule chose que je dois ajouter, c'est la priorité haute ... Voici le code mis à jour ... Peut-il aussi aider quelqu'un :)

 $ch = curl_init("https://fcm.googleapis.com/fcm/send");

    //The device token.
    $token = ""; //token here

    //Title of the Notification.
    $title = "Carbon";

    //Body of the Notification.
    $body = "Bear island knows no king but the king in the north, whose name is stark.";

    //Creating the notification array.
    $notification = array('title' =>$title , 'text' => $body);

    //This array contains, the token and the notification. The 'to' attribute stores the token.
    $arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');

    //Generating JSON encoded string form the above array.
    $json = json_encode($arrayToSend);
    //Setup headers:
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: key= $key'; // key here

    //Setup curl, add headers and post parameters.
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);       

    //Send the request
    $response = curl_exec($ch);

    //Close request
    curl_close($ch);
    return $response;
11
Simerjit Parmar

voici le code testé par moi et fonctionne parfaitement. assurez-vous de passer le jeton fcm

 $path_to_firebase_cm = 'https://fcm.googleapis.com/fcm/send';
    $token=array($token);
   $fields = array(
        'registration_ids' => $token,
        'priority' => 10,
        'data'=>$send_notification ,
        'notification' => array('title' => $type_of_notification, 'body' => $title ,'sound'=>'Default'),
    );
    $headers = array(
        'Authorization:key=' .'your server key' ,
        'Content-Type:application/json'
    );  

    // Open connection  
    $ch = curl_init('https://fcm.googleapis.com/fcm/send'); 
    // Set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $path_to_firebase_cm); 
    curl_setopt($ch, CURLOPT_POST, 1);
    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, json_encode($fields));
    // Execute post   
    $result = curl_exec($ch); 
    // Close connection      
    curl_close($ch);
    return $result;
1
ayush pathak

Semble retourner un succès. Peut-être vérifiez-vous le code d'enregistrement de votre application pour voir si le jeton a été modifié pour le téléphone. Parfois, un nouveau jeton sera généré.

0
Prem Raj