我正在尝试使用带有PHP的Google Firebase Cloud向特定的人发送消息。接收消息的人的令牌存储在DB中。 这是我的代码:
define('SERVER_API_KEY','MYSERVERAPIKEY');
define('FRIEND_TOKEN','FRIEND_TOKEN')
$message = array(
'title'=>'Bet invitation',
'body' =>'Notification from ...',
);
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => FRIEND_TOKEN,
'data' => $message
);
$headers = array(
'Content-Type: application/json',
'Authorization: key='.SERVER_API_KEY
);
$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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
echo "Result: $result";
echo "Curl: ". curl_error($ch);
curl_close($ch);
问题是指定的用户没有收到消息。而且当我通过Firebase控制台对其进行测试时,它可以工作...但是,错误消息也没有显示给我:(
答案 0 :(得分:0)
尝试以下代码,希望对您有所帮助。
$message=[
'title'=>'Bet invitation',
'body' =>'Notification from ...',
];
$send = $this->GoogleCloudMessaging($friend_token,$message);
echo $send;
答案 1 :(得分:0)
我现在自己发现了问题。我将"gcm_sender_id": "YOUR_FIREBASE_PROJECT_SENDER_ID"
添加到manifest.json文件并重写了脚本,这是解决方案:
define('SERVER_API_KEY', 'MYSERVERAPIKEY');
$data = array(
"to" => "$RECIVER_TOKEN",
"notification" => array(
"title" => "Test-Title",
"body" => "Test-Message",
"icon" => "https://url/images/iamge.png",
"click_action" => "https://url")
);
$headers = array(
'Content-Type: application/json',
'Authorization: key=' . SERVER_API_KEY
);
$url = 'https://fcm.googleapis.com/fcm/send';
$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_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
if ($result === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
问候: ashokan,David Angulo和https://santhoshveer.com/web-push-notification/