Android:未点击通知超时

Amb*_*ran 4 notifications android

我将在周末发布通知。如果在一段时间后未看到/点击通知,我希望它自行消失。我找不到在NotificationCompat.Builder表达式中设置超时或类似内容的任何方法。这是(非常正常的通知生成器)代码:

Intent myIntent = new Intent(context, MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.not_bar_small_icon)
                .setColor(ContextCompat.getColor(context, R.color.Green))
                .setContentTitle(context.getResources().getString(R.string.strNotificationTitle))
                .setContentText(context.getResources().getString(R.string.strNotificationMessage))
                .setContentIntent(pendingIntent);

NotificationManager notifyManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notifyManager.notify(MY_NOTIFICATION_ID, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)

这能做到吗?

解决方案更新:

使用下面评论中建议的 Aleksandar 链接解决。

小智 6

https://developer.android.com/reference/android/app/Notification.Builder.html#setTimeoutAfter(long)

您可以指定一个持续时间(以毫秒为单位),在此之后应取消此通知(如果尚未取消)。