问题
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.
回答1:
I had to change
PendingIntent.getActivity
to
PendingIntent.getBroadcast
回答2:
Use method .setContentIntent(), instead of .addAction() to launch pending intent.
来源:https://stackoverflow.com/questions/24864169/notification-action-button-does-not-fire-pending-intent