I record this in the service:
intent = new Intent(this,MainActivity_Large.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this,CHANNEL_ID)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setAutoCancel(true)
.setChannelId(CHANNEL_ID)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.ant_intro)
.setContentTitle("Anttack")
.setContentText("Continues")
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
// .setPriority(2)
.setContentIntent(pendingIntent);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setCategory(Notification.CATEGORY_SERVICE);
}
notification = builder.build();
startForeground(1, notification);
After the device goes to sleep and wakes up, a notification appears on the screen. If you click on it, the minimized activity will be restored.
How can i track completion of PendingIntent?
Maybe someone knows how to use PendingIntent.onFinished?
There are some Activity
methods which will help you to find out whether the Activity
comes to the foreground because the user tapped the notification:
getIntent()
will give you the Intent
which triggered Activity
creation if tapping the notification happens when the Activity
has to be (re)created.
Overriding onNewIntent()
will give you access to the Intent
responsible for re-launching an existing Activity
. Note that you also have the opportunity to set the new Intent
as "the" Intent
for this Activity
:
@Override
protected void onNewIntent(Intent newIntent){
setIntent(newIntent);
}
So you can put a boolean
as Intent
extra when creating the Intent
for your PendingIntent
...
intent = new Intent(this,MainActivity_Large.class);
intent.putExtra("FROM_NOTIFICATION", true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
... and evaluate this in your code, e.g. in Activity.onResume()
@override
protected void onResume(){
if(getIntent().hasExtra("FROM_NOTIFICATION")){
// Activity was (re-)launched because user tapped notification
}
}
I'm aware that this way you won't be able to tell when the user taps the notification a second time when the Activity
is already up and running. Since I don't know your use case I can't tell whether this can happen or whether this would be a problem. But since you can also pass other data as Intent
extra, it should be possible to identify each event if required.