doujing6436 2016-09-15 08:08
浏览 170
已采纳

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

While sending the notification from firebase console Notification is working fine.

firebase console

I am getting push notifications on ios device.

Here is the code that I am using to send push notifications to iphone in php using 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; ?>

And it returns the following response:

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

Please suggest me what I am doing wrong? I use same code for android too with its server key and device token and it is working fine...

  • 写回答

3条回答 默认 最新

  • duanqianwei2485 2016-09-15 09:42
    关注

    Thanks shubank .. your answer works... The only thing I need to add is priority high... Here is the updated code... May it help someone too :)

     $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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化