Android 启动服务的通知操作,不包括额外的意图

Android Notification Action to Start service, intent extra not included

我有一个包含两个操作的通知,AcceptDecline,以及启动服务的 PendingIntentRequestUpdaterService。我在 Intent 中添加了 PendingIntent 的额外内容(通过 PendingIntent#getService)。问题是,当服务启动时,额外内容不在传递给 Service#onStartCommand.

的意图中

通知发布者:

NotificationCompat.Builder n = new NotificationCompat.Builder(context)
                .setDefaults(Notification.DEFAULT_ALL)
                .setSmallIcon(R.drawable.logo_small)
                .setContentTitle(notification.getText())
                .setContentText(context.getString(R.string.app_name))
                .setContentIntent(getOpenAppPendingIntent())
                .setOngoing(false)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(text));

Intent actionAccept = new Intent(context, UpdateRequestUpdater.class);
actionAccept.putExtra(KeysAndCodes.UPDATE_REQUEST_ID, notificationRequestId);
actionAccept.putExtra(KeysAndCodes.UPDATE_REQUEST_NOTIFICATION_ID, notificationId);
actionAccept.setAction(KeysAndCodes.UPDATE_REQUEST_UPDATER_ACCEPT);

PendingIntent acceptPendingIntent = PendingIntent.getService(context, 0, actionAccept, 0);

Intent actionDecline = new Intent(context, RequestUpdater.class);

actionDecline.putExtra(KeysAndCodes.UPDATE_REQUEST_ID, notificationRequestId);

actionDecline.putExtra(KeysAndCodes.UPDATE_REQUEST_NOTIFICATION_ID, notificationId);
actionDecline.setAction(KeysAndCodes.UPDATE_REQUEST_UPDATER_DECLINE);

PendingIntent declinePendingIntent = PendingIntent.getService(context, 0, actionDecline, 0);

n.addAction(R.drawable.ic_action_accept, getString(R.string.accept_request), acceptPendingIntent);
n.addAction(R.drawable.ic_action_cancel, getString(R.string.decline_request), declinePendingIntent);

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notification.getId(), n.build());

RequestUpdaterService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Integer requestId = intent.getIntExtra(KeysAndCodes.UPDATE_REQUEST_ID, 0);
    Integer notificationId = intent.getIntExtra(KeysAndCodes.UPDATE_REQUEST_NOTIFICATION_ID, 0);
...
}

每当 RequestUpdaterService 启动时,requestIdnotificationId 总是 0。我做错了什么?

尝试更改此行:

PendingIntent acceptPendingIntent = PendingIntent.getService(context, 0, actionAccept, 0);

为此:

PendingIntent acceptPendingIntent = PendingIntent.getService(context, 0, actionAccept, PendingIntent.FLAG_UPDATE_CURRENT);