I have an application which in turn runs a foreground service. The application has a start/stop button as part of its notification which as can be guessed starts and stops the foreground service. Upon clicking on the start button a Pending Intent gets triggered.
Consider the following scenario:
The application has been destroyed [removed from the recent items list] but the notification is still visible. I am able to start and stop the foreground service even when the app has been destroyed, as on clicking the notification button triggers a Pending intent (which in turn call a broadcast receiver). But, what I have observed is that after a day or two, upon clicking the buttons on the notification the pending intent is not triggered (that is the foreground service does not start). This begs the question, does pending intent have a lifetime after the encompassing application has been destroyed? Or am I missing something else here?
My pending intent call:
Intent notificationBroadcastIntent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent playIntent = PendingIntent.getBroadcast(this, MY_REQUEST_CODE,
notificationBroadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
The Broadcast Receiver called by the pending intent (which in turn starts the foreground service):
@Override
public void onReceive(Context context, Intent intent) {
Log.i(MyBroadcastReceiver.class.getSimpleName(), "MyBroadcastReceiver called to update service notification");
if (isMyForegroundServiceRunning(MyForegroundService.class, context)) {
Intent stopIntent = new Intent(context, MyForegroundService.class);
stopIntent.setAction(STOP_SERVICE_ACTION);
context.startService(stopIntent);
} else {
Intent startIntent = new Intent(context, MyForegroundService.class);
startIntent.setAction(START_SERVICE_ACTION);
context.startService(startIntent);
}
}
PendingIntent
s do not expire. However, they are not persistent and will not survive a reboot of the device.To see if the
PendingIntent
still exists, you can use the following adb command:adb shell dumpsys activity intents
This lists all of the
PendingIntent
s in the system. You can also look at the notifications using:adb shell dumpsys notification
This will show all
Notification
s, including thePendingIntent
s that are set for them.