Your problem isn’t with the methods you are using to build the Notification
. Your problem is your use of TaskStackBuilder
. Using this class performs a lot of hidden magic behind the scenes and it makes a lot of assumptions about how your want the app to behave. Unfortunately, these assumptions are mostly wrong 🙁
If you just want to bring your application to the foreground in whatever state it was in when it went to the background, you don’t want to use TaskStakBuilder
and you also don’t have to use all those nasty ActivityManager
methods. Just do this:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(YourService.this)
.setContentTitle(getResources().getText(R.string.app_name))
.setContentText(getServiceStateDescription(YourService.this))
.setSmallIcon(iconId)
.setWhen(System.currentTimeMillis());
Intent nIntent = getPackageManager().
getLaunchIntentForPackage(ContextConstants.PACKAGE_NAME);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, nIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
startForeground(ContextConstants.LAUNCHER_SERVICE_NOTE_ID,
notificationBuilder.build());