2

I have a notification in my app and I want that when I tap on the notification:

  • If the app is opened: do nothing;
  • If the app is closed: open the app;

Current code:

Intent intent = new Intent();
intent.setClass(mContext, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent,
    PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(icon);
mBuilder.setContentTitle(string);
mBuilder.setWhen(System.currentTimeMillis());
mBuilder.setOngoing(true);
mBuilder.setContentIntent(contentIntent);
mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
mBuilder.setColor(mContext.getResources().getColor(R.color.theme_color));

Notification notification = mBuilder.build();
mNotificationMgr.notify(id, notification);

Activity manifest:

<activity
    android:name=".MainActivity"
    android:configChanges="keyboardHidden|orientation|mcc|mnc"
    android:launchMode="singleTop"
    android:screenOrientation="portrait"
    android:label="@string/app_name"
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>

If I tap on the notification and the app is already opened, it opens another app in front of the own app! If the app is closed, everything is OK!

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Douglas Fornaro
  • 2,017
  • 2
  • 22
  • 30

2 Answers2

1

This can be done by adding the launchMode for your activity in your manifest.xml For example

<activity android:launchMode = "singleTop"  ../>

Here is an detail example

Kartheek
  • 7,104
  • 3
  • 30
  • 44
1

I'd replace this line:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

With the following:

intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

From the documentation:

public static final int FLAG_ACTIVITY_SINGLE_TOP

If set, the activity will not be launched if it is already running at the top of the history stack.


public static final int FLAG_ACTIVITY_REORDER_TO_FRONT

If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running.

For example, consider a task consisting of four activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then B will be brought to the front of the history stack, with this resulting order: A, C, D, B. This flag will be ignored if FLAG_ACTIVITY_CLEAR_TOP is also specified.

SINGLET_TOP will prevent your activity from being launched again and REORDER_TO_FRONT prevents it from being recreated. Hope it helps.

Community
  • 1
  • 1
Edson Menegatti
  • 4,006
  • 2
  • 25
  • 40
  • How about replacing your launchMode in the manifest to singleTask? – Edson Menegatti Jun 25 '15 at 14:14
  • When you said you wanted the app to do nothing, you meant not receive anything on the onNewIntent method? What are your activities methods that are called after the notification click. – Edson Menegatti Jun 25 '15 at 14:20
  • If i'm in MainActivity and click on notification nothing happen, if I go to BActivity and I click on notification the app is opened in front of the own app and the MainActivity is show again, if I click back the BActivity show again; I want that if i'm in MainActivity or BActivity or XXXXXActivity I want do nothing. – Douglas Fornaro Jun 25 '15 at 14:25
  • You could remove the notification when running the app. That way the notification wouldn't show when the app is running and you wouldn't have this problem. – Edson Menegatti Jun 25 '15 at 14:40
  • Then I don't think you can achieve what you want, unless you change the target class when changing activities. Another option is to redirect the intent to another class that just verifies if the MainActivity is alive and, if not, creates it. – Edson Menegatti Jun 25 '15 at 14:44