2015-04-24 43 views
3

我有一個通知有兩個動作,AcceptDecline,和PendingIntent啓動服務,RequestUpdaterService。我把Intent的額外資料放入PendingIntent(通過PendingIntent#getService)。問題是,當服務啓動時,附加內容不在傳入Service#onStartCommand的意圖中。Android通知操作啓動服務,意圖額外不包括

通知發行人:

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。我究竟做錯了什麼?

回答

1

嘗試改變這一行:

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

要這樣:

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