【问题标题】:how to send push notifications to iphone using fcm(firebase console) in PHP?如何在 PHP 中使用 fcm(firebase 控制台)向 iphone 发送推送通知?
【发布时间】:2017-01-23 04:20:12
【问题描述】:

从 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"}]}

请告诉我我做错了什么?我也为 android 使用相同的代码及其服务器密钥和设备令牌,它工作正常......

【问题讨论】:

  • 你也需要设置'priority' => 'high'
  • 你上传APNS证书了吗?
  • 是的,我已将开发 APNS 证书上传到 firebase

标签: php ios iphone firebase firebase-cloud-messaging


【解决方案1】:

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

【讨论】:

  • 如何将推送通知发送到多个token id??
  • @TusharSaindane 将键名从 to 更改为 registration_ids 并传递数组。
  • 谢谢@Kinshuk Lahiri
【解决方案2】:

这是我测试过的代码,运行良好。确保传递 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;

【讨论】:

    【解决方案3】:

    似乎返回成功。也许检查您的应用注册码以查看手机的令牌是否已更改。有时会生成一个新的令牌。

    【讨论】:

    • 令牌是从手机生成的。我正在使用相同的令牌发送推送通知,但没有在 iphone 上收到
    猜你喜欢
    • 2022-08-22
    • 2018-05-01
    • 1970-01-01
    • 2021-07-15
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2017-11-23
    相关资源
    最近更新 更多