アプリケーションに Firebase Cloud Messaging を実装し、Firebase コンソールを使用している間、Android と iOS のアプリケーションが通知を受け取ります。しかし、私は毎日通知をプッシュしたかったので、サーバー側でそれを行うための cron ジョブを作成しました。cron をトリガーするたびに、アプリケーションがクラッシュすることに気付きました
私の iOS クライアントでは、通知を受け取りません。
私のAndroidクライアントでは、エラーが表示されます:
java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' on a null object reference
FirebaseMessagingService
私のコードはここにあります
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());
}
そして、私のサーバー側で
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;
}
なぜ私は NPE を持っているのか、どうすれば解決できるのでしょうか?