簡體   English   中英

如何在 PHP 中使用 fcm(firebase 控制台)向 iphone 發送推送通知?

[英]how to send push notifications to iphone using fcm(firebase console) in PHP?

從 Firebase 控制台發送通知時,通知工作正常。

Firebase 控制台

我在 ios 設備上收到推送通知。

這是我用來在 php 中使用 FCM 向 iphone 發送推送通知的代碼。

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

它返回以下響應:

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

請告訴我我做錯了什么? 我對 android 也使用相同的代碼及其服務器密鑰和設備令牌,它工作正常......

謝謝 shubank .. 你的回答有效......我唯一需要添加的是優先級高......這是更新的代碼......它也可以幫助某人:)

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

這是我測試過的代碼並且運行良好。 確保傳遞 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;

好像返回成功了。 也許檢查您的應用程序注冊碼以查看手機的令牌是否已更改。 有時會生成一個新的令牌。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM