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!