【发布时间】:2016-06-02 06:54:42
【问题描述】:
我在我的应用程序中实现了 Firebase 云消息传递,并且在使用 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,我该如何解决?
【问题讨论】:
-
$message 中的通知对象在哪里?
-
@sinense 我没有
notification对象,我认为data就足够了,因为notification是可选的。我应该将它添加到我的$message中吗?我应该在notification对象中添加什么? -
是的,通知对象默认是可选的。但是在您的 onMessageReceived() 上,您正在调用 remoteMessage.getNotification() 但您没有要解析的通知对象。
-
@sinense 现在可以使用了,谢谢! :)
标签: android firebase firebase-cloud-messaging