简体   繁体   中英

intent with FCM not working when app is in background(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.

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

用这个:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

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.

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..

In FCM you have two types of messages

  • Notification Messages
  • Data Messages

Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want to process the messages in your client app.

If you need to process your message before sending it to the system tray, it's better to use Data messages, as for these types of messages, the callback first reaches the onMessageRecieved method before going to the system tray.

IN YOUR SERVICE

 "to": token, 
 "notification": {
     "title": "Title,
     "body": "Body"        
 },                    
"data" : {
     "update": "yes"
 }

IN ANDROID KOTLIN

val intent = Intent(this,MainActivity::class.java)
intent.putExtra("update","yes")
......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM