intent with FCM not working when app is in background(android)

how to handle notification when app in background in firebase android
how to handle notification when app in foreground in firebase android
push notification not working in background android
firebase notification not working in background
android fcm notification click open activity
android background notification
fcm app background
handle fcm push notification when app is not running android

I am using FCM to push notification. I am passing intent to launch new activity when notification is clicked.when app is in foreground,app works fine and intent launch new activity, but when app is in background, it does not launch new activity but launch instance of default activity.

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());

    //Calling method to generate notification
    sendNotification(remoteMessage.getNotification().getBody());
}
private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, SecActivity.class);
    intent.putExtra("started_from","notification");
    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.ic_launcher)
            .setContentTitle("Firebase Push Notification")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

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

}

Hope you are trying to launch the mainactivity when the message is received. When the app is resumed from background your current activity is getting cleared. From the documentation for FLAG_ACTIVITY_CLEAR_TOP: If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

Try removing this flag.

FCM when app in background · Issue #20 · firebase/quickstart-android, I got it working as you have mentioned from the intent of the activity. "FCM. initialData" , where it used when app is not running. not only FCM background notifications works on all the android versions except Android 7. The FCM works fine when the app is in foreground and a push comes, but when the app is in background and the system tray notification is clicked , it just opens up the app without passing any intent extras. I have added the click_action in Notification Payload from Advanced option of Firebase Console

I too had this same problem but i managed to have it fix with this ,

In your default activity mentioned in the manifest do this in the onCreate

if (bundle != null) {
    if ((String) bundle.get("tag") != null) {
        String tag = (String) bundle.get("tag");
        if (tag.equals("abc")) {
            Intent intent = new Intent(SplashActivity.this, MessageDetailsActivity.class);
            startActivity(intent);
        } else if (tag.equals("def")) {
            openSpecificActivity(tag, (String) bundle.get("id"));
        }
    } else {
        Intent i = new Intent(SplashActivity.this, HomeActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
    }
}

Google FCM getIntent returns null when app is not running or when , Google FCM getIntent returns null when app is not running or when app is in When your app is in the background, Android directs notification  ---> extras is always null when app is not running or when app is in background} @Override public void onNewIntent(Intent intent) { Bundle extras = intent.getExtras(); ---> extras is always null when app is not running or when app is in background } Remarks: The data is available when App State is in the Foreground.

i got a solution for that. just put below code in oncreate method of launcher activity.

if (bundle != null) {
        String value = bundle.getString("key");
        if (value != null) {

            startActivity(new Intent(MainActivity.this, secActivity.class));
        }
}

when app is in background or killed,FCM will not call onmessagerecieved method,but it will send data to system tray to display notification.so datapayload(sent from fcm console) will not be handled by onmessagerecieved method.when user click on notification,it will launch default activity of app and datapayload will be passed by intent .so making change in oncreate method of launcher activity(as above)we can get datapayload even when app is in background or killed.(ex key is sent by fcm console).when app is in foreground datapayload and will be handled by onmessagerecieved method of fcm service.

Receive messages in an Android app, Notification messages delivered when your app is in the background. system tray, and the data payload is delivered in the extras of the intent of your launcher Activity. as Android O's background execution limits may interfere with your ability to complete your work. In some situations, FCM may not deliver a message. Experiencing the same issues but on ionic 1. Works fine with the app is in the foreground but background or closed nothing comes up from adb logcat | grep PushPlugin except the registration stuff when you tap the notification from closed. If the app receive the notification while the app is in the foreground.

Based upon Antinio's answer

https://stackoverflow.com/a/37845174/4454119

Why is this happening?

There are two types of messages in FCM (Firebase Cloud Messaging):

display-messages: These messages trigger the onMessageReceived() callback only when your app is in foreground

data-messages: Theses messages trigger the onMessageReceived() callback even if your app is in foreground/background/killed Firebase team have not developed a UI to send data-messages to your devices, yet.

So you need to use data-messages..

Android FCM notification not showing in system tray, Hello, My FCM notification is not working when from Intercom, i can send test by Intercom, notification dosen't appear in system tray when app is in background​. <intent-filter> <action android:name="com.google.firebase. DanikKamilov commented Nov 4, 2017. onMessageRecieved not working when app is killed. It works perfect in foreground and in background and I can receive Extras in MainActivity. But when app is killed I receive notification and after click on it- Extras == null.

Secrets of Firebase Cloud Messaging, Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you the app is open and running (currently in use); Background — When the app is open and not in use. <category android:name="android.intent.category. Hi, I'm using RN 33 and fcm 2.1.0. When I send data only messages while my app is running in the background the app receives the messages. When I send notifications with or without data and/or content_available the notifications get rece

Push Notifications with Firebase Cloud Messaging, Take your Android skills to the next level with this free tutorial! Then Firebase does the work behind the scenes to get the Under the same tag you can also add metadata for default notification values, but it is not mandatory: 1 2 3 4 by MyFirebaseMessagingService when the app is in the background! Enable background services and Jobs (or FCM) in Chinese ROMs val intent = Intent() val manufacturer = android.os This is not the recommended way but is a work-around to make your app work

Handle FCM messages on Android, When your app is in the foreground, the FCM notification message is You can then use the data in getIntent() to update the app content if needed. Note: if the user dismisses the notification, data will not be delivered when app opens. on background work complete, update the notification if still active } }. There are multiple reasons such as Unregistered device token or app is killed or Android Oreo’s doze mode is on etc. We will talk about here for one reason of app being in background and foreground and see how it can be solved. Firebase Notifications applies different mechanisms when app is foreground and when app is in background.

Firebase Notifications in Background & Foreground in Android, But when they integrate, there are few common issues which they stuck into. When developers need to integrate push notifications in their Android apps, the first <intent-filter> <action android:name=”com.google.firebase. Working easily with FCM push notifications in Android. then I wrote how to fix it and still work with FCM just changing a couple of things. if the app if is background,

Comments
  • You should come to this thread for the solution this
  • not going in first if condition.bundle is null after click of notification.
  • Only if bundle has the tag title and data it will be a valid notification from fcm.This will work when you add these value to the push