3

My Android App consists of a single Activity containing two fragments accessed with tabs (I'm using a ViewPager).

Inside Fragment #2, a notification is created from a button. This button starts a background task that needs to continue if the user sends the app to background with the home button, for example.

I want the user to be able to bring back the application to the front when clicking the notification, exactly as if the app icon was used. Here is my code :

Notification.Builder mBuilder =
    new Notification.Builder(getActivity())
    .setSmallIcon(R.drawable.ic_stat_av_play)
    .setContentTitle("MyApp")
    .setContentText("A background task is running");

Intent notificationIntent = new Intent(getActivity(), MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);


PendingIntent notificationPendingIntent = PendingIntent.getActivity(
    getActivity(), 0, notificationIntent, 0);

mBuilder.setContentIntent(notificationPendingIntent);
mNotificationManager = (NotificationManager) getActivity().getSystemService(
    Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());

Unfortunately, clicking the notification closes the currently running application and starts it again.

As you can see, I have tried the solution proposed here: Resume application and stack from notification, but it does not work. Many similar questions also suggest to use one or more of these flags:

FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_REORDER_TO_FRONT

I found out that none of these has any effect on my notification behavior. Is there something I am missing to make this work as intended?

Edit:

Maybe this will help you to help me. The problem really seems to come from the app structure.

If I replace this line :

Intent notificationIntent = new Intent(getActivity(), MainActivity.class);

by this one :

Intent notificationIntent = new Intent(getActivity(), Fragment2.class);

The notification behavior will be correct if it is clicked while the app is still in the foreground (ie the notification drawer will simply close itself) However, if I return to the home screen with the home button, and then click the notification, nothing happens.

If I use the first intent definition (with MainActivity.class), none of the flags has any effect on the intent. This also applies to the launchMode defined in the manifest.

Edit 2

Solution added. I'm not too sure about why it is working, so any additional input would be appreciated!

Community
  • 1
  • 1
Iodestar
  • 198
  • 2
  • 13

5 Answers5

1

I use this method and problem is resolved. Hope this can help you too.

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClass(this, Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Carl
  • 251
  • 1
  • 4
  • 13
  • This is the only thing that's worked for me so far. It might be worth noting I still needed `Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP` along with it though. – aProperFox Apr 04 '18 at 17:05
0

Can you try this,

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP
                | Intent.FLAG_ACTIVITY_NEW_TASK);

And in your manifest,

android:launchMode="singleTask"

for your MainActivity.

Chitrang
  • 5,097
  • 1
  • 35
  • 58
  • This didn't work. It really looks like the flag(s) I use are not recognized for some reason. No matter what I try, my notification behavior stays the same. – Iodestar May 14 '14 at 12:05
  • One try with removing, this two lines of code, notificationIntent.setAction(Intent.ACTION_MAIN); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); and put only what I have defined. – Chitrang May 14 '14 at 15:40
  • May be, i am assuming this error, put android:launchMode="singleTask" in your tag of your MainActivity. Not in your tag. I made this mistak previously. – Chitrang May 15 '14 at 04:17
  • The launchMode was at the right place, inside the activity definition in the manifest ! – Iodestar May 15 '14 at 14:53
0

So, I finally managed to make it work! Here is the final code, I hope it will help others who are in a similar situation.

mBuilder =
    new Notification.Builder(getActivity())
    .setSmallIcon(R.drawable.ic_stat_av_play)
    .setContentTitle("MyApp")
    .setContentText("A background task is running.")
    .setOngoing(true);

notificationIntent = new Intent(Intent.ACTION_MAIN);
notificationIntent.setClass(getActivity().getApplicationContext(), MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

notificationPendingIntent = PendingIntent.getActivity(getActivity(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

mBuilder.setContentIntent(notificationPendingIntent);

mNotificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(1, mBuilder.build());

I also had to set this launchMode in the manifest:

android:launchMode="singleTop"
Iodestar
  • 198
  • 2
  • 13
0

you can try it,maybe help you!

Intent intent = new Intent(Intent.ACTION_MAIN);

intent.addCategory(Intent.CATEGORY_LAUNCHER);

intent.setClass(this, Main.class);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Carl
  • 251
  • 1
  • 4
  • 13
0

The only way it worked for me was getting the launch intent from PackageManger :

import android.content.pm.PackageManager;
import android.content.Context;
import android.content.Intent;


String packageName = mContext.getPackageName();
        PackageManager pm = mContext.getPackageManager();
        Intent launchIntent = pm.getLaunchIntentForPackage(packageName);
return PendingIntent.getActivity(mContext, REQUEST_CODE, launchIntent,
                PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_CANCEL_CURRENT);
Kaki Master Of Time
  • 1,428
  • 1
  • 21
  • 39