0

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

}

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
don 2
  • 1
  • 3
  • You should come to this thread for the solution [this](http://stackoverflow.com/questions/42976152/how-to-auto-launch-android-app-when-you-receive-notification-fcm/42980848#42980848) – Vishvendu Palawat Mar 24 '17 at 10:54

7 Answers7

0

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.

fightingCoder
  • 594
  • 3
  • 7
0

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);
    }
}
g7pro
  • 817
  • 1
  • 6
  • 11
  • not going in first if condition.bundle is null after click of notification. – don 2 Mar 21 '17 at 06:14
  • 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 – g7pro Mar 21 '17 at 06:51
0

Use this:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Pang
  • 9,564
  • 146
  • 81
  • 122
Patel Jaimin
  • 231
  • 1
  • 11
0

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.

don 2
  • 1
  • 3
0

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

Community
  • 1
  • 1
Nitin Jain
  • 1,314
  • 11
  • 15
0

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.

fightingCoder
  • 594
  • 3
  • 7
0

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")
......
Ray
  • 3,864
  • 7
  • 24
  • 36
Irvin Joao
  • 1,010
  • 8
  • 8