这是我的用例。如果结束时间与当前时间之间的差异大于60s,并且结束时间与当前时间之间的差异为60s及以下,则我会生成不同类型的通知。
我有一个名为showChronometer
的函数,用于处理各种基于通知的布局。但是,用例是,如果用户刷了一个通知,则我们将关闭该通知,并且永远不会再次看到带有通知ID的通知。
不幸的是,即使删除挂起的意图消除了通知管理器的通知。通知被取消后,警报管理器仍会生成通知
public Notification makeNotification()
{
final NotificationCompat.Builder builder = makeNotificationBuilder();
if (builder == null)
return null;
final RemoteViews collapsedView = makePrimaryView(COLLAPSED_RES_ID);
builder.setContent(collapsedView);
if (rawNotification.getTitle() != null)
builder.setContentTitle(rawNotification.getTitle());
if (rawNotification.getBody() != null)
builder.setContentText(rawNotification.getBody());
PendingIntent cpi = getPendingClickIntent();
if (cpi != null)
builder.setContentIntent(cpi));
PendingIntent dpi = getDeletePendingIntent();
if (dpi != null)
builder.setDeleteIntent(dpi);
addButtons();
builder.setAutoCancel(true);
return builder.build();
}
这是生成警报管理器的代码。我希望能够阻止警报管理器在触发删除操作时生成任何警报。
这是我的警报管理器代码。
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent expireHandler = injectNotificationAlarmReceiverIntent();
long now = System.currentMilliseconds();
long timeDiff = endingTime * DateUtils.SECOND_IN_MILLIS - now;
// If more than 60 seconds are remaining, the show the text in normal style.
PendingIntent initialPendingIntent = injectPendingIntent(expireHandler);
PendingIntent pendingIntentAtOneMinute = injectPendingIntent(expireHandler);
if (now < (endingTime * DateUtils.SECOND_IN_MILLIS - DateUtils.MINUTE_IN_MILLIS))
{
alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
endingTime * DateUtils.SECOND_IN_MILLIS - DateUtils.MINUTE_IN_MILLIS, initialPendingIntent);
showChronometer(rootView, timeDiff, false);
alarmManager.cancel(initialPendingIntent);
}
else if (now < endingTime * DateUtils.SECOND_IN_MILLIS)
{
alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP, endingTime * DateUtils.SECOND_IN_MILLIS,
pendingIntentAtOneMinute);
showChronometer(rootView, timeDiff, true);
alarmManager.cancel(pendingIntentAtOneMinute);
}
else
{
rootView.setViewVisibility(R.id.notification_countdown, View.GONE);
rootView.setViewVisibility(R.id.notification_countdown_60s, View.GONE);
rootView.setViewVisibility(R.id.notification_time_expired, View.VISIBLE);
// Chronometer is enabled.
rootView.setTextViewText(R.id.notification_content, "text body");
}
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
PendingIntent injectPendingIntent(Context context, Intent intent)
{
return PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_ONE_SHOT);
}