Search code examples
androidnotificationsbackground-foreground

On Notification Click put my app to background


I am working on an functionality where I get notification for POI (Point of Interest )while the user is navigating through Google Navigation app (used background service to get location update ).

scenario

1) User clicks navigate button from my app

2) He is taken to Google Navigation with data to navigate

3) Now user receives Notification for POI , then he clicks on it and he is taken to my app (my app brought to foreground , still Google Navigation i in background)

4) Now user is seeing my app , now he clicks the same notification , In this case I must put my app to background , so that use now sees Google Navigation

what i tried

Bringing my app to foreground is achieved , but putting my app to background on Notification click is not achieved .

moveTaskToBack(true) this is what I came across, but how can I implement this in Notification click

code

Intent intent = new Intent();
        intent.setComponent(new ComponentName(getPackageName(), B_Activity.class.getName()));
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("toFront", toFront);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Solution

  • moveTaskToBack(boolean bool) is a method Of Activity class . So you can only call it with activity reference. I don't thing that there is way to do it with intent . What you can do send a broadcast in notification intent, and then validate the intent in activity if its in foreground -> call movetaskBack().

    If your B_Activity has no Ui . Then just set null component in Pending Intent. Use Local Broadcast to notify your currently running activity to call moveTaskToBack().

    Intent intent = new Intent();
    intent.setAction("com.settaskback");//Your custom action goes here
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    

    Then send broadcast on notification click. The activity which will receive this broadcast will put task to background (only if its in forground)

    final Intent broadcastIntent = new Intent("com.settaskback");
            PendingIntent intent = PendingIntent.getBroadcast(getApplicationContext(), 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            notificationBuilder.setContentIntent(intent);
            notification = notificationBuilder.build();
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            NotificationManager mNotificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(1, notification);
    

    You need to register a Local broadcast receiver for this action in Activity.

    @Override
        public void onReceive(Context context, Intent intent) {
              if(somecondition){
                  moveTaskToBack(true);
              }
        }