我正在尝试使用两个操作创建通知 - Dismiss和Snooze但是从不调用第一个操作。我正在通过以下方式尝试通知:
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(reminder.getName())
.setContentText(reminder.getNote())
.setLargeIcon(bigIcon)
.setWhen(when)
.setColor(ContextCompat.getColor(context, R.color.primary))
.setSound(Uri.parse(ringtonePreference))
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(reminder.getName()));
之后,我将为解雇行动创建待处理的意图。
private static PendingIntent createDismissIntent(Context context, GenericModel model) {
int notificationId = ((Reminder) model).getNotificationId();
Intent dismissIntent = new Intent(context, DismissReceiver.class);
dismissIntent.putExtra(NOTIFICATION_ID, notificationId);
dismissIntent.putExtra(MODEL_ID, model);
return PendingIntent.getBroadcast(context, notificationId, dismissIntent, 0);
}
然后我将为暂停操作创建待处理的意图。
private static PendingIntent createSnoozeIntent(Context context, GenericModel model) {
int notificationId = ((Reminder) model).getNotificationId();
Intent snoozeIntent = new Intent(context, SnoozeReceiver.class);
snoozeIntent.putExtra(NOTIFICATION_ID, notificationId);
return PendingIntent.getBroadcast(context, notificationId, snoozeIntent, 0);
}
然后我将这两个操作添加到通知构建器。
builder.addAction(android.R.color.transparent, context.getResources().getString(R.string.dismiss), createDismissIntent(context, model));
builder.addAction(android.R.color.transparent, context.getResources().getString(R.string.snooze), createSnoozeIntent(context, model));
之后,我为整个通知创建待定意图。
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(notificationId, PendingIntent.FLAG_UPDATE_CURRENT);
我还为每个动作创建了两个广播接收器,但其中一个从未被调用过。