mori mori - 6 months ago 1120
Android Question

How to implement firebase cloud messaging in server side?

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.

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.

Here's my code

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;


}

Answer

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.

Comments