I try to send intent to BroadcastReceiver from notification action button my problem is that when I click the button the intent in not fired or not getting to the BroadcastReceiver.
Here is my code:
The notification:
Intent accept = new Intent(this, ChallengesActionReceiver.class);
accept.setAction("com.soinfit.utilities.CHALLENGE_CLICK");
accept.putExtra("reqId", extras.getString("reqId"));
accept.putExtra("answer", "1");
PendingIntent acceptIntent = PendingIntent.getActivity(this, 1, accept, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(image)
.setSmallIcon(R.drawable.app_icon_small)
.setContentTitle("soInFit")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setAutoCancel(true)
.addAction(R.drawable.app_icon_small, getString(R.string.accept), acceptIntent)
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
Notification nof = mBuilder.getNotification();
nof.defaults|= Notification.DEFAULT_SOUND;
nof.defaults|= Notification.DEFAULT_LIGHTS;
nof.defaults|= Notification.DEFAULT_VIBRATE;
nof.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);
notificationManager.notify(id+1, nof);
The BroadcastReceiver
public class ChallengesActionReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
Log.i("test", "test");
}
}
Manifest:
<receiver
android:name="utilities.ChallengesActionReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="com.soinfit.utilities.CHALLENGE_CLICK" />
</intent-filter>
If I call this code:
Intent accept = new Intent(this, ChallengesActionReceiver.class);
accept.setAction("com.soinfit.utilities.CHALLENGE_CLICK");
accept.putExtra("reqId", extras.getString("reqId"));
accept.putExtra("answer", "1");
PendingIntent acceptIntent = PendingIntent.getActivity(this, 1, accept, PendingIntent.FLAG_CANCEL_CURRENT);
this.sendBroadcast(accept);
Its working fine.
I had to change
PendingIntent.getActivity
to
PendingIntent.getBroadcast
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With