2016-06-02 8 views
4

Firebase Cloud Messagingを自分のアプリケーションに実装しました.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'オブジェクトを持っていません。' notification'はオプションなので 'data'で十分だと思います。私は '$ message'にそれを追加すべきでしょうか? 'notification'オブジェクトに何を置くべきですか? – natsumiyu

+0

yes通知オブジェクトは、デフォルトではオプションです。しかし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()); 
} 
関連する問題