0

What I wanted to do was open the app by notification in two cases:

  1. Case open app : Just pull background

  2. Case closed app: Opens new app

The code that I have is the next:

NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

String title = context.getString(R.string.notification_title);
String message = context.getString(getNotificationText());
Bitmap largeIcon = getLargeBitmap();

NotificationCompat.Builder notification =
        (NotificationCompat.Builder) new NotificationCompat.Builder(context).setContentTitle(title).setContentText(message)
       .setSmallIcon(getIcon()).setLargeIcon(largeIcon).setOngoing(true);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {

        Intent intent = new Intent(context, MainActivity.class);
        intent.setAction("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.LAUNCHER");


        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(intent);

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setContentIntent(contentIntent);
    }
    manager.notify(NETWORK_STATUS_NOTIFICATION_ID, notification.build());
}

The problem I have is that the code is working on Android 5 and up, but does not work on Android : JELLY_BEAN and KITKAT .

Someone help me?

Pedro Simões
  • 309
  • 2
  • 11

1 Answers1

0

Add flag FLAG_ACTIVITY_NEW_TASK to your intent :

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

From the docs :

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in.

Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39