Notification action button does not fire pending i

2019-02-22 11:33发布

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.

2条回答
走好不送
2楼-- · 2019-02-22 12:21

I had to change

PendingIntent.getActivity

to

PendingIntent.getBroadcast
查看更多
Luminary・发光体
3楼-- · 2019-02-22 12:21

Use method .setContentIntent(), instead of .addAction() to launch pending intent.

查看更多
登录 后发表回答