Firebase Cloud Messaging PHP



我正在尝试使用带有PHP的Google Firebase Cloud向特定人员发送消息。接收消息的人员的令牌存储在数据库中。 这是我的代码:

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控制台对其进行测试时,它可以工作...但是,奇怪的是,错误消息也没有显示给我:(

尝试下面的代码,希望它会有所帮助。

$message=[
'title'=>'Bet invitation',
'body' =>'Notification from ...',
];
$send = $this->GoogleCloudMessaging($friend_token,$message);
echo $send;

我现在自己发现了问题。我将"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);

问候: 阿育王、大卫·安古洛和 https://santhoshveer.com/web-push-notification/

相关内容

  • 没有找到相关文章

最新更新