Skip to content

How to handle FCM Push notification when app is in background or killed in Android Oreo? #765

@yaramvenki

Description

@yaramvenki

Question 1: How to handle FCM Push notification when app is in background or killed in Android?

  • I implemented FCM push notifications, i am getting data as well notification data like remoteMessage.getNotification() and remoteMessage.getData().size() > 0. Now, i am able to get data from both when app is in foreground. How to get both/single data when app is in background?

Question 2: I am getting notification when app is in background, but on click of that notification it is not launching activity? how to implement that?(Android)

implementation 'com.google.firebase:firebase-core:16.0.6'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
public class HealthAppMessagingService extends FirebaseMessagingService {
    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
    }
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        if (remoteMessage.getNotification() != null) {
            title = remoteMessage.getNotification().getTitle();
            messageBody = remoteMessage.getNotification().getBody();
        }
        if (remoteMessage.getData().size() > 0) {
            try{
                type = remoteMessage.getData().get("type");
                time = remoteMessage.getData().get("time");
              
                Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
                resultIntent.putExtra("message", messageBody);
                showNotificationMessage(title, messageBody, time, resultIntent, null);
            } catch (Exception e){
                Log.e(TAG, "Exception: " + e.getMessage());
            }
        }
    }
}

How to get notification data and data when app is in background? how to implement code for that?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions