In Android, PendingIntents are the standard way for notifications (and other system components) to trigger actions in your app (activities, services, or broadcasts). The reason your PendingIntents are being reused is because by default, the system tries to reuse them based on Intent "equality." This can cause issues if you expect different intents for different connections (IDs) but they are treated as the same.
Why PendingIntent reuse happens
PendingIntent reuse is based on the Intent's action, data, type, class, and categories — extras are not considered in comparison. So if you create multiple PendingIntents with only different extras (e.g., different connection IDs), they may be considered the same, leading to reuse and potentially the wrong data in your callbacks.
Can you call service methods directly from notification actions instead of PendingIntent?
No. Notification actions cannot call methods in your service directly. The Android notification action buttons must use PendingIntents that start an Activity, Service, or BroadcastReceiver. The system uses these to communicate user interaction back to your app.
How to fix your problem
1. Differentiate PendingIntents so they are unique
You must create distinct PendingIntents so that the system treats them as unique.
- Use different request codes when calling
PendingIntent.getService() or getBroadcast()
- Use different unique data URIs or action strings in Intents
- Use
PendingIntent.FLAG_UPDATE_CURRENT or FLAG_MUTABLE / FLAG_IMMUTABLE according to your target SDK
- Compose the Intent carefully: add a unique data Uri like this:
Intent intent = new Intent(context, YourService.class);
intent.setAction("com.example.ACTION_ACCEPT"); // or REJECT
intent.putExtra("connection_id", connectionId);
// Make the Intent unique by setting a data Uri that encodes your connection ID
intent.setData(Uri.parse("custom://" + connectionId + "/accept"));
PendingIntent pi = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Each distinct connectionId will create a unique Intent because of different data URIs, so the PendingIntent won’t be reused incorrectly.
2. Use BroadcastReceiver
If you want more flexibility and easier control, have the notification action send a broadcast, handled by a BroadcastReceiver. The receiver then calls into the service.
Intent intent = new Intent(context, YourBroadcastReceiver.class);
intent.setAction("com.example.ACTION_ACCEPT");
intent.putExtra("connection_id", connectionId);
intent.setData(Uri.parse("custom://" + connectionId + "/accept"));
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Then inside your BroadcastReceiver, you can start or communicate with your service as needed.
3. Use different request codes
If you want, the easiest simple fix is to pass a different requestCode for each PendingIntent:
int requestCode = (int) connectionId; // or hash it if needed
PendingIntent pi = PendingIntent.getService(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Summary
- You cannot directly call service methods from notification actions without
PendingIntent.
- Use unique
PendingIntents by differentiating Intent data/action or request codes.
- Consider using
BroadcastReceiver-based intents to decouple.
- Avoid expecting Intent extras to make the PendingIntent unique; system ignores extras when matching.
If you want, I can help you with a minimal working example for creating unique PendingIntents for your notification actions. Just ask!