I have a custom button in a sticky notification.
I used to attach a PendingIntent
to it for receiving button clicks:
Intent intent = new Intent();
intent.setAction("com.example.app.intent.action.BUTTON_CLICK");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 2000, intent, PendingIntent.FLAG_UPDATE_CURRENT);
contentViewExpanded.setOnClickPendingIntent(R.id.button, pendingIntent);
When i run this code on Oreo , i get BroadcastQueue: Background execution not allowed
in logcat and don't receive button click.
I registered receiver with manifest:
<receiver
android:name=".BroadcastReceiver.NotificationActionReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.example.app.intent.action.BUTTON_CLICK"/>
</intent-filter>
</receiver>
I also tried registering receiver in my code:
NotificationActionReceiver mMyBroadcastReceiver = new NotificationActionReceiver();
IntentFilter filter = new IntentFilter("com.example.app.intent.action.BUTTON_CLICK");
mContext.registerReceiver(mMyBroadcastReceiver, filter);
This works but only when the app is visible to user.
Thanks for help
I ran into this issue as well on
Android 8 - Oreo
, but given my library project requirements, I don't have the explicitly namedBroadcastReceiver
class implementation, that the end-client will declare in it'sAndroidManifest
.Solution:
Specify the application package on the
Intent
using setPackage(String).Example:
From the Android Broadcasts: Security considerations and best practices docs.
Here’s an example of the
BroadcastReceiver
declared (or merged) in to the end-client application’sAndroidManifest
:Since my example revolves around a library project that broadcasts an
Intent
, I’ve decided to keep the<intent-filter>
and<action />
in the<receiver>
declaration. Otherwise, there would be non-unique broadcast actions being fired, which could lead to potential issues where multiple applications receive the wrong broadcast. This is mostly a safety precaution. Of course you still need to check the action in the implementation of theBroadcastReceiver
.Hope someone finds this helpful!
Never use an implicit
Intent
when an explicitIntent
will work.Replace:
with:
And remove the
<intent-filter>
from theNotificationActionReceiver
<receiver>
element.