0

I am building an app with fragments and I am using Firebase notifications. I want, when user click on notification, to send him to the Fragment1 on MainActivity. And, I've done it, but the problem is that works only when app is in the foreground. When app is in background notification sends me to the MainActivity not to the Fragment1. This is MyFirebaseMessagingService:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.d(TAG, "From " + remoteMessage.getFrom());
    Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
    sendNotification(remoteMessage);
    Log.d("Msg", "Poruka je stigla");
}
private void sendNotification(RemoteMessage remoteMessage){
    Intent intent=new Intent(myFirebaseMessagingService.this, MainActivity.class);
    intent.putExtra("action", "goToFragment1");
    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 notification=new NotificationCompat.Builder(this)
            .setSmallIcon(logo)
            .setContentText(remoteMessage.getNotification().getBody())
            .setContentTitle("Naslov")
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification.build());



}}

And this is my MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dugme=(Button)findViewById(R.id.dugme1);
    dugme2=(Button)findViewById(R.id.subscribe);
    dugme.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Fragment fragment = null;
            if(view==dugme) {
                fragment = new Fragment1();
            }
            FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.myFragment, fragment);
            transaction.addToBackStack(null);
            transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
            transaction.commit();
        }
    });
    String msg=getIntent().getStringExtra("action");
    FragmentManager fragmentManager=getSupportFragmentManager();
    FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
    if (msg!=null){
        if (msg.equals("goToFragment1")){
            Fragment1 fragment1=new Fragment1();
            fragmentTransaction.replace(R.id.myFragment, fragment1);
            fragmentTransaction.commit();
        }
    }

    dugme2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FirebaseMessaging.getInstance().subscribeToTopic("android");
            Log.d("Log", "Uspesno ste se pretplatili");
        }
    });

}

What should I do?

Goran_1992
  • 103
  • 1
  • 2
  • 9

2 Answers2

1

When your app is in background onNewIntent will be called instead of onCreate.

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    String msg=intent.getStringExtra("action");
    FragmentManager fragmentManager=getSupportFragmentManager();
    FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
    if (msg!=null){
       if (msg.equals("goToFragment1")){
           Fragment1 fragment1=new Fragment1();
           fragmentTransaction.replace(R.id.myFragment, fragment1);
           fragmentTransaction.commit();
       }
    }
}
Ravi
  • 34,851
  • 21
  • 122
  • 183
  • No, again sends me to the first page(Main Activity) – Goran_1992 Jun 07 '16 at 13:04
  • @RRR is right, onNewIntent will be called instead of onCreate and therefore you will have to handle the fragment selection in onNewIntent. If you have doubts, just check by adding Logs :) – Doppie Jun 07 '16 at 13:18
  • But what I must write in onNewIntent() when this code above doesn not change anything. – Goran_1992 Jun 07 '16 at 13:24
  • I have logged this onNewIntent and it hasn't been showing nothing. – Goran_1992 Jun 07 '16 at 13:58
  • What should I do to fix the problem which I have...when app is instaled for the first time, I cant go to fragment with notification, I just go on first page on Main Activity.. – Goran_1992 Jun 09 '16 at 07:35
  • No I debugged the app but the `onNewIntent()` method is not called when the app is resumed from background. – viper Mar 07 '17 at 06:55
0

You can use BroadCastReceiver in your fragment. https://developer.android.com/reference/android/content/BroadcastReceiver.html?hl=ru

Try to create something like this:

Override onResume method

IntentFilter filter = new IntentFilter();
filter.addAction("SOME_STRING_CONSTANT");
getActivity().registerReceiver(receiver, filter);

and unregister your receiver in onDestroy method. Now your broadcast receiver can works in background

EDIT

private BroadcastReceiver receiver = new BroadcastReceiver() {

  @Override
  public void onReceive(Context context, Intent intent) {
    if (intent.getAction() == "SOME_STRING_CONSTANT") {
        // do something
    } 
  }
};

@Override
public void onDestroyView() {
   super.onDestroyView();
   getActivity().unregisterReceiver(receiver);
}

You can send an event to broadcast receiver from anywhere

Intent intent = new Intent();
intent.setAction("SOME_STRING_CONSTANT");
startActivity(intent);
giffell
  • 1
  • 2