0

Firebase Notification works perfect. I get the notifications, but I have a problem. I want to open a specific activity after clicking on the notification. If the app is open, it works perfect. But if the app is closed, the MainActivity opens. I don't know why. Can somebody help me. What must I change? Thanks!

Here's the code of onMessageReceived:

public class MessageService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Intent intent=new Intent(this,activity2.class); //To this activity it should also go when the app isn't open

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
    notificationBuilder.setContentTitle("App");
    notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSmallIcon(R.drawable.icon);
    notificationBuilder.setContentIntent(pendingIntent);
    NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,notificationBuilder.build());


}
}

EDIT

WelcomeActivity (launcher activity):

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
    if (getIntent().hasExtra("type")) {
        Intent intent=new Intent(this, activity2.class);
        intent.putExtra("type",getIntent().getStringExtra("type"));
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    } else {
        startActivity(new Intent(this, MainActivity.class));
    }
}
android
  • 33
  • 4

2 Answers2

0

Notification messages delivered when your app is in the background: In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.

From https://firebase.google.com/docs/cloud-messaging/android/receive

One approach is to add the 'click_action' flag which will tell the system which activity to open when the user clicks on the notification. See https://stackoverflow.com/a/37656244/3482621

Also see https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support

albert c braun
  • 2,650
  • 1
  • 22
  • 32
0

when the application is closed or is in background the data from the notification arrives at your launcher activity in the intent. You can put a check in oncreate() that if your intent has data then open desired activity otherwise simple opens launcher activity or main activity whatever be your launcher activity.

Devil10
  • 1,853
  • 1
  • 18
  • 22
  • That means that I always send other data with the notification? – android Jun 11 '17 at 19:54
  • no the notification contains the flags or the specified data type with which you can distinguish the notification. by putting that notification in check you can do your further implementation... – Devil10 Jun 11 '17 at 19:58
  • Can you show me the code how you mean this, please? – android Jun 11 '17 at 20:01
  • Where I must write this code? In onMessageReceived? – android Jun 12 '17 at 19:16
  • Put this code in oncreate method of your launcher activity which opens very first when application is opened – Devil10 Jun 13 '17 at 03:09
  • I did it, but it doesn't work. Do you maybe know what can be my mistake? Thanks! – android Jun 13 '17 at 19:46
  • I write my code in my question. Can you check it please? – android Jun 14 '17 at 11:44
  • the code in your question can only be called when your application is in foreground not in case when app is in background or closed – Devil10 Jun 14 '17 at 11:46
  • Yes, I write the code I have now with your news in the Edit and then tell me please whats wrong. – android Jun 14 '17 at 11:53
  • sir further code will be written in the launcher activity of your project from where data arrives atfirst when push arrives – Devil10 Jun 14 '17 at 11:55
  • Now I added the code from hastebin.com/ezisigadux.scala you gave me in the Welcome Activity. But it doesn't work. Thanks for your help! – android Jun 14 '17 at 11:56
  • WelcomeActivity is my launcher activity – android Jun 14 '17 at 11:57
  • in my suggestion the "type" is the parameter in which i am passing data which help me to distinguish the type of push... you have to debug the push data and then perform the function – Devil10 Jun 14 '17 at 11:59
  • I don't really understand. Can you show me a code how you intend it? Then your answer gets the check! – android Jun 14 '17 at 12:01
  • go to your firebase console and create a push to you application, and in the additional properties add some data with key-"title" and data whatever you want then debug the code from here in the welcome activity... basically the type is the key of data you are sending from the backend or firebase console – Devil10 Jun 14 '17 at 12:07
  • THANKS! That works! Very nice. I check your answer! Thanks very much – android Jun 14 '17 at 12:14
  • are you satisfied with answer.. what was the problem. – Devil10 Jun 14 '17 at 12:16
  • I'm very satisfied. My problem was that I didn't send the data with the notification. – android Jun 14 '17 at 12:34
  • Thanks you too :) – android Jun 14 '17 at 12:40