2016-06-02 4 views
4

Firebase 클라우드 메시징을 내 애플리케이션에 구현했으며 Firebase 콘솔을 사용하는 동안 Android 및 iOS의 내 애플리케이션에서 내 알림을받습니다. 그러나 매일 알림을 보내려고했기 때문에 내 서버 쪽에서 cron 작업을 만들었습니다. 내 cron 응용 프로그램이 다운 될 때마다 응용 프로그램이 작동하지 않을 때가 있습니다.remoteMessage.getNotification(). getBody()의 오류

내 iOS 클라이언트에서 알림을받지 못합니다. 내 안드로이드 클라이언트에서

이 오류가 표시

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가 있고 어떻게 c가되는지 궁금합니다. 나는 그것을 해결합니까?

+0

$ 메시지의 알림 개체는 어디에 있습니까? – mgcaguioa

+0

@sinense'notification' 객체가 없기 때문에'data'만으로 충분하다고 생각합니다. 왜냐하면'notification'은 선택 사항이기 때문입니다. '$ message'에 추가해야합니까? 'notification' 객체에는 무엇을 넣어야합니까? – natsumiyu

+0

예 알림 개체는 기본적으로 선택 사항입니다. 하지만 onMessageReceived()에서는 remoteMessage.getNotification()을 호출하지만 구문 분석 할 알림 개체가 없습니다. – mgcaguioa

답변

17

$ 메시지에 알림 개체를 추가하십시오. 당신의 POST 요청의 본문 알림 개체가 포함되어 있지 않기 때문에

{ 
    "to" : "aUniqueKey", 
    "notification" : { 
     "body" : "great match!", 
     "title" : "Portugal vs. Denmark" 
    }, 
    "data" : { 
     "Nick" : "Mario", 
     "Room" : "PortugalVSDenmark" 
    } 
} 

귀하의 remoteMessage.getNotification() 반환 null : 당신의 POST 요청의 몸은 뭔가를해야합니다.

Use notifications when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want your app to handle the display or process the messages on your Android client app, or if you want to send messages to iOS devices when there is a direct FCM connection.

참조 용으로 Documentation for Advanced Messaging Options을 확인하십시오.

+0

당신은 최고입니다 –

-1
if (remoteMessage.getNotification() != null) { 
    sendNotification(remoteMessage.getNotification().getBody()); 
}