我的通知包含两项操作:Accept
和Decline
,以及启动服务的PendingIntent
RequestUpdaterService
。我将额外内容添加到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
开始时,requestId
和notificationId
始终为0
。我做错了什么?
答案 0 :(得分:1)
尝试更改此行:
PendingIntent acceptPendingIntent = PendingIntent.getService(context, 0, actionAccept, 0);
对此:
PendingIntent acceptPendingIntent = PendingIntent.getService(context, 0, actionAccept, PendingIntent.FLAG_UPDATE_CURRENT);