如何使用PHP在FCM(Firebase控制台)向iPhone发送推送通知?

10

从Firebase控制台发送通知时,通知正常工作。

firebase控制台

我在iOS设备上收到推送通知。

这是我使用FCM在PHP中向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"}]}

请给我建议,我做错了什么?我使用相同的代码在安卓上也用它的服务器密钥和设备标记,它正常工作......


5
你需要将 'priority' => 'high' 也设置好。 - Shubhank
你已经上传了APNS证书吗? - Gandharv
是的,我已经将开发APNS证书上传到Firebase。 - Simerjit Parmar
3个回答

18

感谢 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;

如何将推送通知发送到多个令牌ID? - Tushar Rmesh Saindane
1
@TusharSaindane 将键名从 to 更改为 registration_ids 并传递数组。 - Kinshuk Lahiri
感谢 @Kinshuk Lahiri - Tushar Rmesh Saindane

4

以下是我测试过并且完美运行的代码。请确保传递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
似乎返回了成功。可以检查您的应用程序注册代码,查看电话号码的令牌是否已更改。有时会生成新的令牌。

2
令牌是从手机生成的。我正在使用相同的令牌发送推送通知,但在iPhone上没有收到。 - Simerjit Parmar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接