I have applied Firebase Cloud Messaging in my application and, using the Firebase console, my application in Android and iOS receives my notifications. But since I wanted to push the notification daily, I created a cron job to do this on my server side. I notice that every time I run my cron, my application crashes
In my iOS client, it does not receive any notifications.
An error message appears in my android client:
java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' on a null object reference
Where is it in my FirebaseMessagingServicehere is my code
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}
And on my server side
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;
}
I am wondering why I have NPE and how can I solve it?