0

When I send a notification message with data payload on foreground, my application crashes. It works fine when it is in background or killed state but why it doesn't work on foreground?

This is the error:

java.lang.NullPointerException at ... MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:57)

and the error indicates the line given in MyFirebaseMessagingService:

if(dataType.equals(getString(R.string.direct_message))){

Compiler already catches the problem and it says:

Method invocation 'equals' may produce 'java.lang.NullPointerException'

This is a small part of Firebase Messaging Service class:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";
    private static final int BROADCAST_NOTIFICATION_ID = 1;

    @Override
    public void onDeletedMessages() {
        super.onDeletedMessages();
    }

    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        String notificationBody = "";
        String notificationTitle = "";
        String notificationData = "";
        try{
           notificationData = remoteMessage.getData().toString();
           notificationTitle = remoteMessage.getNotification().getTitle();
           notificationBody = remoteMessage.getNotification().getBody();
        }catch (NullPointerException e){
           Log.e(TAG, "onMessageReceived: NullPointerException: " + e.getMessage() );
        }
        Log.d(TAG, "onMessageReceived: data: " + notificationData);
        Log.d(TAG, "onMessageReceived: notification body: " + notificationBody);
        Log.d(TAG, "onMessageReceived: notification title: " + notificationTitle);


        String dataType = remoteMessage.getData().get(getString(R.string.data_type));
        if(dataType.equals(getString(R.string.direct_message))){
        Log.d(TAG, "onMessageReceived: new incoming message.");
            String title = remoteMessage.getData().get(getString(R.string.data_title));
            String message = remoteMessage.getData().get(getString(R.string.data_message));
            String messageId = remoteMessage.getData().get(getString(R.string.data_message_id));
            sendMessageNotification(title, message, messageId);

        }
    }
}

I guess (getString(R.string.direct_message) is null but I don't know why it is null on foreground. How can I solve the problem?

Cem
  • 13
  • 8
  • `dataType` must be null, you could add a breakpoint to check it – Juan Cruz Soler May 06 '19 at 15:07
  • 1
    whenever there is a crash - you should post the logs - in this case from logat - with log trace its much easy to see the class's method/code causing the crash – AADProgramming May 06 '19 at 15:20
  • `if(dataType != null && dataType.equals(getString(R.string.direct_message))){` – Frank van Puffelen May 06 '19 at 15:27
  • I've checked the logs and `dataType` is null as you said and I also updated if statement as you write @Frank. However, if I do this, app doesn't crash but it doesn't get the notification either. My aim is it getting notification on foreground too. Is it possible? – Cem May 06 '19 at 15:53
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Ricardo A. May 06 '19 at 16:39

0 Answers0