2017-05-08 6 views
-1

私のコードでこのfirebaseプッシュ通知を使用すると、通知メッセージが表示されますが、そのあとアプリケーションは残念ながら閉じられません。致命的なエラーjava.lang.NullPointerException RemoteMessage $ Notification。 getBody() 'をnullオブジェクト参照で使用します。どのようにこのエラーを克服するには?

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

private static final String TAG = "MyFirebaseMsgService"; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    //Displaying data in log 
    //It is optional 
    Log.d(TAG, "From: " + remoteMessage.getFrom()); 
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 
    sendNotification(remoteMessage.getNotification().getBody()); 
    //Calling method to generate notification 
    /*if (remoteMessage.getNotification() != null) { 
     sendNotification(remoteMessage.getNotification().getBody()); 
    }*/ 

    if (remoteMessage.getFrom() != null) { 

     sendNotification(remoteMessage.getNotification().getBody()); 
    } else { 

     String SMessage = "Hello this is used for the testing"; 
     sendNotification(SMessage); 
    } 


} 

//This method is only generating push notification 
//It is same as we did in earlier posts 
private void sendNotification(String messageBody) { 
    Intent intent = new Intent(this, SBTVerfyLogin.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.notify) 
      .setContentTitle("Firebase Push Notification") 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.notify(0, notificationBuilder.build()); 
} 
} 
+0

PHPコードを投稿できますか?おそらく、ペイロードに通知本体の値を含めないことになります。 –

答えて

0

まず、あなたは二回sendNotificationを(remoteMessage.getNotification()。getBody())を呼び出しています。また、私の推奨はgetBody()のnullをチェックして送信することです。 2回の呼び出しは避けてください。

関連する問題