PHP + Android CURL Firebase通知错误 [英] PHP + Android CURL Firebase Notification Error

查看:843
本文介绍了PHP + Android CURL Firebase通知错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从PHP发送通知到Android应用程式。我的PHP代码在这里。

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

并且我发布一个数据到mycode我给出一个输出:

and i post a data to mycode i give an this output :

我可以从FireBase控制台发送通知。和服务器的id代码是正确的。我不能是什么事情?你能给一个建议或任何方式来解决这个问题吗?谢谢...

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

推荐答案

这是测试代码,您可以使用以下代码发送通知。

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

这篇关于PHP + Android CURL Firebase通知错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆