douzhong6480 2016-08-30 14:31
浏览 55

PHP + Android CURL Firebase通知错误

I'm trying to send a notification from PHP to Android App. My PHP CODES are here.

<?php
require "int.php";
$message = $_POST['message'];
$title = $_POST['title'];
$path_to_fcm = 'https://fcm.googleapis.com/fcm/send';
$server_key = "1:485869546397:android:bd503a78b6c26c35";
$sql = "select fcm_token from fcm_info";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_row($result);
$key = $row[0];
$headers = array(
    'Authorization:key=' .$server_key,
    'Content-Type:application/json'
);

$fields = array('to'=>$key,
    'notification'=>array('title'=>$title,'body'=>$message));

$payload = json_encode($fields);

echo $payload;

$curl_session = curl_init();
curl_setopt($curl_session, CURLOPT_URL, $path_to_fcm);
curl_setopt($curl_session, CURLOPT_POST, true);
curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_session, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload);
$result = curl_exec($curl_session);
echo $result;
?>

and i post a data to mycode i give an this output : enter image description here

And i can send a notification from FireBase Console. And Server id code is correct.I could not what is the matter ? Can you give an advice or any way to solve this problem ? Thank you...

  • 写回答

1条回答 默认 最新

  • drm16022 2016-11-17 10:48
    关注

    This is tested code and you can send notification with below code.

    public function sendGCM($message, $registration_ids) {
        //FCM URL
        $url = "https://fcm.googleapis.com/fcm/send";
    
        //prepare data
        $fields = array (
            'registration_ids' => array ($registration_ids),
            'data' => array ("message" => $message)
        );
        $fields = json_encode ( $fields ); 
    
        //header data
        $headers = array ('Authorization: key=<YOUR_API_KEY>', 'Content-Type: application/json');
    
        //initiate curl request
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_POST, true );
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
    
        // execute curl request
        $result = curl_exec ( $ch );
    
        //close curl request
        curl_close ( $ch );
    
        //return output
        return $result;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序