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

标签 php ios iphone firebase firebase-cloud-messaging

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

firebase console

我在 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 及其服务器 key 和设备 token ,并且工作正常......

最佳答案

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

关于php - 如何在 PHP 中使用 fcm(firebase 控制台)向 iphone 发送推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39506040/

相关文章:

php - 在 Woocommerce 单个产品页面上显示 dokan 供应商名称

php - 关联数组

ios - Swift:在展开期间 performSegueWithIdentifier 不起作用

javascript - 在触摸手机上使用 Javascript 检测手指拖动?

php - 无法使用 FPDF 将文件保存到目录

gd - php gd lib 已安装但无法正常工作

ios - 将 UITextField 链接到 UITableViewCell 子类时出现问题

android - Calabash 支持 iOS 和 Android 版本吗?

iphone - 我在哪里可以找到用于 Objective-C 的 CSV 到 NSArray 解析器?

iphone - 方向改变