如何在服务器端实现Firebase云消息传递?
迁移到Firebase之后,我测试了使用firebase控制台发送通知的方法,它可以正常工作,但是我需要在特定时间发送每日通知,因此我不使用Firebase控制台,而是使用以前的cron作业每天发送通知.我将 https://android.googleapis.com/gcm/send
更改为 https://fcm.googleapis.com/fcm/send
,但是我的设备没有收到任何通知.
After migrating to Firebase, i tested sending notification by using the firebase console it works fine, but i need a daily notification on a specific time so instead of using the firebase console i use my former cron job to send notification daily. I changed https://android.googleapis.com/gcm/send
to https://fcm.googleapis.com/fcm/send
but my device doesn't receive any notification.
有什么办法解决这个问题?还是我错过了什么?我的Cron工作对于仍在使用GCM的设备运行正常.
Is there any way to solve this? Or did I miss anything? my cron job is working fine for my devices that is still using GCM.
这是我的代码
function sendNotificationFCM($apiKey, $registrationIDs, $messageText,$id) {
$headers = array(
'Content-Type:application/json',
'Authorization:key=' . $apiKey
);
$message = array(
'registration_ids' => $registrationIDs,
'data' => array(
"message" => $messageText,
"id" => $id,
),
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://fcm.googleapis.com/fcm/send',
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode($message)
));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
我在json中添加了 notification
对象.我发现在我的 remoteMessage.getNotification().getBody()
中,它返回null,这就是为什么它不接收我的cron发送的任何通知的原因.
I added notification
object in my json.
I found out that in my remoteMessage.getNotification().getBody()
it returns null that's why it doesn't receive any notification send by my cron.
修改
这是我的json对象
$message = array(
'registration_ids' => $registrationIDs,
'notification' => array(
"title" => $id,
"body" => $messageText,
"icon" => "name_of_icon" ),
'data' => array(
"message" => $messageText,
"id" => $id,
),
);