通知操作按钮不会触发待处理的意图

时间:2014-07-21 11:38:08

标签: android android-intent android-notifications android-pendingintent android-broadcast

我尝试从通知操作按钮向BroadcastReceiver发送意图我的问题是当我点击按钮时意图没有被触发或没有到达BroadcastReceiver。

这是我的代码:

通知:

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); 

BroadcastReceiver

public class ChallengesActionReceiver  extends BroadcastReceiver

{

    @Override
    public void onReceive(Context context, Intent intent) {

       Log.i("test", "test");
}

}

清单:

<receiver
    android:name="utilities.ChallengesActionReceiver"
    android:enabled="true" >

     <intent-filter>
    <action android:name="com.soinfit.utilities.CHALLENGE_CLICK" />     
       </intent-filter>

如果我拨打此代码:

    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);

工作正常。

2 个答案:

答案 0 :(得分:6)

我不得不改变

PendingIntent.getActivity

PendingIntent.getBroadcast

答案 1 :(得分:-1)

使用方法.setContentIntent()而不是.addAction()来启动待处理的意图。