如何在PHP中使用fcm

如何在PHP中使用fcm

本文介绍了如何在PHP中使用fcm(firebase控制台)将推送通知发送到iphone?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





我在ios设备上收到推送通知。 / p>

这里是我使用的代码,发送推送通知到iphone使用FCM在PHP中。

 <?php $ ch = curl_init(https://fcm.googleapis.com/fcm/send); 

//设备令牌。
$ token =;

//通知的标题。
$ title =Carbon;

//通知的正文。
$ body =熊岛不认识国王,而是北方的国王,他的名字是鲜明的。

//创建通知数组。
$ notification = array('title'=> $ title,'text'=> $ body);

//这个数组包含令牌和通知。 'to'属性存储令牌。
$ arrayToSend = array('to'=> $ token,'notification'=> $ notification);
//从上面的数组生成JSON编码的字符串。
$ json = json_encode($ arrayToSend);

//安装标题:
$ headers = array();
$ headers [] ='Content-Type:application / json';
$ headers [] ='Authorization:key = abcdgfdk'; //服务器键在这里

//设置卷曲,添加标题和发布参数。
curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,POST);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ json);
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);

//发送请求
$ response = curl_exec($ ch);

//关闭请求
curl_close($ ch);
返回$ response; ?>

然后返回以下回应:

  { multicast_id:7847791275395796141, 成功:1, 失败:0, canonical_ids:0 结果:[{ MESSAGE_ID:0:1473926169782959% 51b989d251b989d2}]} 

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

解决方案

感谢shubank。你的答案有效...我需要添加的唯一东西是优先级高...这里是更新后的代码...可以帮助别人:)

  $ ch = curl_init(https://fcm.googleapis.com/fcm/send); 

//设备令牌。
$ token =; //标记在这里

//通知的标题。
$ title =Carbon;

//通知的正文。
$ body =熊岛不认识国王,而是北方的国王,他的名字是鲜明的。

//创建通知数组。
$ notification = array('title'=> $ title,'text'=> $ body);

//这个数组包含令牌和通知。 'to'属性存储令牌。
$ arrayToSend = array('to'=> $ token,'notification'=> $ notification,'priority'=>'high');

//从上面的数组中生成JSON编码的字符串。
$ json = json_encode($ arrayToSend);
//安装标题:
$ headers = array();
$ headers [] ='Content-Type:application / json';
$ headers [] ='Authorization:key = $ key'; //在这里键

//设置卷曲,添加标题和发布参数。
curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,POST);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ json);
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);

//发送请求
$ response = curl_exec($ ch);

//关闭请求
curl_close($ ch);
返回$ response;


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

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...

解决方案

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;

这篇关于如何在PHP中使用fcm(firebase控制台)将推送通知发送到iphone?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:40