how to send push notifications to iphone using fcm(firebase console) in PHP?(如何在 PHP 中使用 fcm(firebase 控制台)向 iphone 发送推送通知?)
问题描述
从 firebase 控制台发送通知时,通知工作正常.
While sending the notification from firebase console Notification is working fine.
我在 ios 设备上收到推送通知.
I am getting push notifications on ios device.
这是我用来使用 FCM 在 php 中向 iphone 发送推送通知的代码..
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 = "Q2FyYm9u";
    //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 使用相同的代码及其服务器密钥和设备令牌,它工作正常......
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...
推荐答案
谢谢 shubank .. 你的答案有效... 我唯一需要添加的是优先级高... 这是更新的代码... 可以也帮助别人:)
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 = "Q2FyYm9u";
    //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 发送推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 PHP 中使用 fcm(firebase 控制台)向 iphone 发送推送通知?
				
        
 
            
        - 从 PHP 中的输入表单获取日期 2022-01-01
 - 正确分离 PHP 中的逻辑/样式 2021-01-01
 - Laravel 仓库 2022-01-01
 - 带有通配符的 Laravel 验证器 2021-01-01
 - 没有作曲家的 PSR4 自动加载 2022-01-01
 - SoapClient 设置自定义 HTTP Header 2021-01-01
 - Mod使用GET变量将子域重写为PHP 2021-01-01
 - 如何定位 php.ini 文件 (xampp) 2022-01-01
 - Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
 - PHP Count 布尔数组中真值的数量 2021-01-01
 
				
				
				
				