创建带有到期日期的 Android 通知

编程入门 行业动态 更新时间:2024-10-12 01:28:17
本文介绍了创建带有到期日期的 Android 通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在 Android 中创建一个具有到期日期的通知,这意味着在某个日期,如果它没有打开,它将被自动销毁或删除.这可能吗?有人知道怎么做吗?

I would like to create a notification in Android that has an expiration date, meaning that on a certain date, if it's not open, it will be automatically destroyed or removed. Is this possible? Does someone knows how to do this?

感谢您的帮助.

推荐答案

如果你有通知 ID,你可以通过调用 NotificationManager.cancel.要实现过期,您可以使用 AlarmManager 设置警报以唤醒 BroadcastReceiver ,这将简单地取消通知.(如果通知不再存在,则取消调用将什么也不做.)

You can remove your own app's notifications if you have the notification ID by calling NotificationManager.cancel. To implement the expiration, you can set an alarm with AlarmManager to wake up a BroadcastReceiver that will simply cancel the notification. (If the notification is no longer there, then the call to cancel will do nothing.)

// post notification notificationManager.notify(id, notification); // set up alarm AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, MyBroadcastReceiver.class); intent.setAction("com.your.package.action.CANCEL_NOTIFICATION"); intent.putExtra("notification_id", id); PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // note: starting with KitKat, use setExact if you need exact timing alarmManager.set(..., pi);

在您的 BroadcastRecevier...

In your BroadcastRecevier...

@Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if ("com.your.package.action.CANCEL_NOTIFICATION".equals(action)) { int id = intent.getIntExtra("notification_id", -1); if (id != -1) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(id); } } }

更多推荐

创建带有到期日期的 Android 通知

本文发布于:2023-10-31 09:06:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545674.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   通知   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!