通知操作按钮待定意图不起作用

时间:2018-10-17 10:11:23

标签: android notifications android-notifications localnotification

我在本地通知上添加了两个操作按钮。我已经为每个动作设置了待定意图,并带有一些参数并调用广播接收器。在广播接收器的onReceive方法上,当我尝试获取intent.getStringExtra()每次都返回null。我知道这是一个简单的问题,但我缺少一些我找不到的东西。非常遗憾。

添加操作按钮:

Intent takenIntent = new Intent(context, NotificationReceiver.class);
        takenIntent.setAction(Constants.TAKEN);
        Toast.makeText(context,time,Toast.LENGTH_LONG).show();
        takenIntent.putExtra(Constants.TIME,time);
        Log.e(Constants.TIME,time);


        PendingIntent takenPendingIntent =
                PendingIntent.getBroadcast(context, 0, takenIntent, 0);

        Intent notTakenIntent = new Intent(context, NotificationReceiver.class);
        notTakenIntent.putExtra(Constants.TIME,time);
        notTakenIntent.setAction(Constants.TAKEN);

        PendingIntent notTakenIntentPendingIntent =
                PendingIntent.getBroadcast(context, 0, notTakenIntent, 0);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, id)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(AppUtils.get12HrsDate(time))
                .setAutoCancel(true)
                .setGroupSummary(true)
                .setGroup(GROUP_KEY)
                .setContentIntent(pendingIntent)
                .setChannelId(id)
                .addAction(R.drawable.ic_drug_profile,context.getString(R.string.taken), takenPendingIntent)
                .addAction(R.drawable.ic_drug_profile,context.getString(R.string.not_taken), notTakenIntentPendingIntent);

NotificationReceiver广播:

public class NotificationReceiver extends BroadcastReceiver {
    private static final String TAG = NotificationReceiver.class.getSimpleName();

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

        if(intent != null){
            String action = intent.getAction();
            if(action != null){
                // always return null
                String time = intent.getStringExtra(Constants.TIME);
                if(action.equalsIgnoreCase(Constants.TAKEN)){
                    Toast.makeText(context,action + " " + time,Toast.LENGTH_LONG).show();
                    Log.e(TAG,action);
                }else if(action.equalsIgnoreCase(Constants.NOT_TAKEN)){
                    Toast.makeText(context,action + " " + time,Toast.LENGTH_LONG).show();
                    Log.e(TAG,action);
                }
            }
        }
    }
}

AndroidManifest.xml

<receiver android:name=".receiver.NotificationReceiver" />

我放置了调试器,并在执行putExtra时检查时间值不为null。

我想念什么吗?

0 个答案:

没有答案