如何在安卓系统中设定特定时间后移除通知?

11

我正在创建一款简单的安卓应用程序,并在其中创建了一个通知。

现在,如果用户在特定时间内没有响应,我想要移除该通知。

也就是说,我想在五分钟后移除通知。


可能是几秒钟后清除通知的重复问题。 - mike47
5个回答

23

请大家阅读此文档链接:https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#setTimeoutAfter(long) - 如果您需要使用 API 26 之前的版本,则此功能将无法使用。 - Daniel Wilson

6
你需要的是AlarmManager和NotificationManager的组合。
注册AlarmManager,它将在5分钟后调用一个服务,并在服务实现中使用NotificationManager.cancel。
Alarm Service示例在这里。我假设你知道如何使用Notification Manager

2

AlarmManager更常用于需要在应用关闭时在后台运行的复杂服务。对于简单的小线程,您也可以在Handler中使用经典的Java Runnable。

Handler h = new Handler();
    long delayInMilliseconds = 5000;
    h.postDelayed(new Runnable() {
        public void run() {
            mNotificationManager.cancel(id);
        }
    }, delayInMilliseconds);

还可以在这里查看:

几秒钟后清除通知

您也可以使用TimerTask类。


1
在上述答案中,只有这个对我起作用。 - Noor Hossain
这种方法只在延迟大约1分钟或更短的情况下才适用于我的情况。 - AndrazP

1
在 Android 版本大于等于 8.0(Oreo)时,我们可以使用以下内容:

NotificationCompat.Builder#setTimeoutAfter(long)

对于 Android < 8.0,我们可以使用 AlarmManager。
将以下内容添加到 AndroidManifest.xml 中:
<receiver
    android:name=".AutoDismissNotification"/>

创建AutoDismissNotification.kt文件并添加以下代码:
class AutoDismissNotification : BroadcastReceiver() {

    companion object {
        private const val KEY_EXTRA_NOTIFICATION_ID = "notification_id"
    }

    override fun onReceive(context: Context, intent: Intent) {
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.cancel(intent.getIntExtra(KEY_EXTRA_NOTIFICATION_ID, 0))
    }

    fun setAlarm(context: Context, notificationId: Int, time: Long) {
        val alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        val alarmIntent = Intent(context, AutoDismissNotification::class.java)
        alarmIntent.putExtra(KEY_EXTRA_NOTIFICATION_ID, notificationId)
        val alarmPendingIntent = PendingIntent.getBroadcast(context, notificationId, alarmIntent, PendingIntent.FLAG_ONE_SHOT)
        alarmMgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + time, alarmPendingIntent)
    }

    fun cancelAlarm(context: Context, notificationId: Int) {
        val alarmIntent = Intent(context, AutoDismissNotification::class.java)
        alarmIntent.putExtra(KEY_EXTRA_NOTIFICATION_ID, notificationId)
        val alarmPendingIntent = PendingIntent.getBroadcast(context, notificationId, alarmIntent, PendingIntent.FLAG_ONE_SHOT)
        val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        alarmManager.cancel(alarmPendingIntent)
    }
}

在构建通知时,请添加以下内容:

long timeOut = 5 * 1000L; // Five seconds
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    builder.setTimeoutAfter(timeOut);
}
else {
    AutoDismissNotification().setAlarm(this, notificationId, timeOut);
}

0
您可以像这样创建通知。
 NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
                .setSmallIcon(Util.getNotificationIcon(context))
                .setContentTitle(new SessionManager().getDomainPreference(context))
                .setAutoCancel(true)
                .setOngoing(false)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setShowWhen(false)
                .setContentText(summaryText)
                .setTimeoutAfter(3000) // add time in milliseconds
                .setChannelId(CHANNEL_ID);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接