Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

click on notification to go current activity

i am using this solution : How to make notification intent resume rather than making a new intent?

I works fine when i run my app normally.. however my app has a share function.

When i select the images i want to share from gallery app and it open it in my activity, i create a notification in the activity. I want that when user clicks on the notification it opens the existing activity (opened by the gallery app)

The problem is that when i click on the notification it does not resume that activity but instead open a new instance of the activity or another instance already opened previously

Edit: More explanation

My app is called ShareApp and the activity is called ShareActivity the problem is that when i open the ShareActivity via gallery app, an instance of the ShareActivity is created at the top of the gallery app task's stack. Now when i create a notification that points to my ShareActivity, i use the intent:

Intent intent = new Intent(this, ShareActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

problem is that when i click on the notification, it points to the instance of the ShareActivity in the the ShareApp task's stack instead of the gallery app task's stack..

any idea how to point to the correct task's stack??

edit 2: my code

        int defaults = Notification.FLAG_NO_CLEAR;
        NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(getString(R.string.app_name))
                        .setContentText(text)
                        .setDefaults(defaults);
        Intent intent = new Intent(this, this.getClass());
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        mBuilder.setContentIntent(pendingIntent);
        NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(_ID, mBuilder.build());

Edit 3 : adb shell dumpsys activity (after applying the code by David https://stackoverflow.com/a/16901603/1334268)

My app is called shareApp and my activity is the ShareActivity

Before clicking on notification:

Main stack:
    TaskRecord{41965708 #6 A com.android.gallery3d}
    Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery3d/.app.Gallery bnds=[364,50][108,108]}
      Hist #2: ActivityRecord{417ccc28 com.yeahman.shareapp/.ShareActivity}
        Intent { act=android.intent.action.SEND_MULTIPLE typ=image/* cmp=com.yeahman.shareapp/.ShareActivity (has extras) }
        ProcessRecord{41c868d0 6088:com.yeahman.shareapp/10116}
      Hist #1: ActivityRecord{4135e540 com.android.gallery3d/.app.Gallery}
        Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery3d/.app.Gallery bnds=[364,50][108,108] }

After clicking notification:

 Main stack:
    TaskRecord{41965708 #6 A com.android.gallery3d}
    Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery3d/.app.Gallery bnds=[364,50][108,108]}
      Hist #3: ActivityRecord{4169f358 com.android.gallery3d/.app.Gallery}
        Intent { flg=0x20000000 cmp=com.android.gallery3d/.app.Gallery bnds=[0,205][480,301] }
        ProcessRecord{4175dd28 5808:com.android.gallery3d/10036}
      Hist #2: ActivityRecord{417ccc28 com.yeahman.shareapp/.ShareActivity}
        Intent { act=android.intent.action.SEND_MULTIPLE typ=image/* cmp=com.yeahman.shareapp/.ShareActivity (has extras) }
        ProcessRecord{41c868d0 6088:com.yeahman.shareapp/10116}
      Hist #1: ActivityRecord{4135e540 com.android.gallery3d/.app.Gallery}
        Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery3d/.app.Gallery bnds=[364,50][108,108] }
like image 857
yeahman Avatar asked Jun 02 '13 18:06

yeahman


People also ask

How do I make my Android notifications clickable?

1 Answer. Show activity on this post. setContentIntent(PROVIDE_YOUR_PENDING_INTENT); You will need to provide a pending intent to where you want to redirect the user when he click on the notification.

What is pending intent in notification?

Android PendingIntent In other words, PendingIntent lets us pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.


4 Answers

    Notification.Builder mBuilder =
            new Notification.Builder(this)
            .setSmallIcon(R.drawable.cmplayer)
            .setContentTitle("CoderoMusicPlayer")
            .setContentText("PLayer0!");

    Intent resultIntent = new Intent(this, AndroidBuildingMusicPlayerActivity.class);
    resultIntent.setAction(Intent.ACTION_MAIN);
    resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            resultIntent, 0);

    mBuilder.setContentIntent(pendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());

Just Copy the code and paste it in your main launcher activity.

like image 81
Hasan Masud Avatar answered Oct 01 '22 02:10

Hasan Masud


Set

android:launchMode="singleTop"

in your manifest.xml for your main activity and see if that helps.

like image 22
Ken Wolf Avatar answered Oct 01 '22 01:10

Ken Wolf


If you have multiple instances of ShareActivity active in different tasks, there is no way for you to tell Android which task to reactivate by launching your activity. It sounds to me like what you want to do, in this case, is to launch not your activity, but the Gallery application. If the Gallery app is already running and your activity is on the top of the task stack, then all you need to do is to bring the Gallery app to the foreground. To do this, create a "launch Intent" for the Gallery app and make sure to set Intent.FLAG_ACTIVITY_NEW_TASK on the Intent. You should be able to get a "launch Intent" from the PackageManager by using getLaunchIntentForPackage()

EDIT: Example code using ActivityManager

You might try this:

// Get the root activity of the task that your activity is running in
ActivityManager am = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
ActivityManager.RunningTaskInfo task = tasks.get(0); // Should be my task
ComponentName rootActivity = task.baseActivity;

// Now build an Intent that will bring this task to the front
Intent intent = new Intent();
intent.setComponent(rootActivity);
// Set the action and category so it appears that the app is being launched
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

NOTE: You will need GET_TASKS permission for this. I haven't tried this and can't guarantee it will work. The use of getRunningTasks() is discouraged by the documentation, but that doesn't mean that it won't suit your purposes.

EDIT added action/category to the Intent

NOTE: I actually built a small app to test this. I needed to add the ACTION=MAIN and CATEGORY=LAUNCHER to the Intent (which would be there if you used PackageManager.getLaunchIntentForPackage(). I've got an HTC One S, so on my device the "Gallery" app is actually called com.htc.album/.AlbumMain.ActivityMainDropList, but this should still work for you.

like image 22
David Wasser Avatar answered Oct 01 '22 01:10

David Wasser


This is a solution I've been using for years.

Your problem is that your PendingIntent starts it's target from an appless content.

Your first step is to bring the result of your Intent inside your apps context. To do this have your notification call a broadcast to "ForwardingReceiver"

Intent newIntent = new Intent(context, ForwardingReceiver.class);
...
mBuilder.setContentIntent(PendingIntent.getBroadcast(context, category, newIntent, PendingIntent.FLAG_UPDATE_CURRENT));

The ForwardingReceiver extends BroadcastReceiver and in it's onReceive() method you now have you're apps current context and can use sendOrderedBroadcast() like so:

Intent forwardIntent = new Intent();
forwardIntent.putExtras(intent.getExtras());
forwardIntent.setAction(GenericReceiver.class.getCanonicalName());
context.sendOrderedBroadcast(forwardIntent, null);

Now, the fun part. You need to extend all of your apps Activities from a common Activity parent. You may already do this for logging or analytic. In this CommonActivityParent onResume() you need to register to receive broadcasts at a priority of above 0 (such as 10)

mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        performActionUsingIntent(intent);
    }
};

IntentFilter filter = new IntentFilter();
filter.addAction(GenericReceiver.class.getCanonicalName());
filter.setPriority(10);
registerReceiver(mReceiver, filter);

(Remember to unregister this in onPause())

This will mean that if your app is in the foreground, it will receive the notification click (indirectly) and allow you to perform an action.

However, if you Activity is in the background, it won't have this receiver registered. No problem, your "GenericReceiver" should look something like this

@Override
public void onReceive(Context context, Intent intent) {
    PackageManager pm = context.getPackageManager();
    Intent newIntent = pm.getLaunchIntentForPackage(context.getPackageName());
    newIntent.putExtras(intent.getExtras());
    context.startActivity(newIntent);
}

Which will start your launch activity. This launch activity should have code in it's onCreate() to detect if it's the tasks root. If it isn't it should close immediately (and therefore is never visible to the user). This effectively forces the last open task into the foreground:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (!isTaskRoot()
            && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
            && getIntent().getAction() != null
            && getIntent().getAction().equals(Intent.ACTION_MAIN)) {

        // Use this space to add the intent to a static singleton. This will be used in CommonActivityParent to perform an action.
        CommonActivityParent.sIntent = getIntent();
        finish();
        return;
    }
}

To finish, ensure just after your registerReceiver() call above in CommonActivityParent, you perform the following check:

if(sIntent != null) {
     Intent tempIntent = sIntent;
     sIntent = null;
     performActionUsingIntent(tempIntent);
}

An hey presto - You're app will now act as if a notification click is actually a click from within your app. And all it took was a hell of a lot of work!

Hopefully someone has a less convoluted way of doing this.

like image 26
Graeme Avatar answered Oct 01 '22 02:10

Graeme