Seb*_*anT 5 notifications android android-notifications
我想在Android中创建一个具有过期日期的通知,这意味着在某个特定日期,如果它没有打开,它将被自动销毁或删除.这可能吗?有人知道怎么做吗?
谢谢你的帮助.
如果您有通知ID,则可以通过调用删除自己的应用程序通知NotificationManager.cancel.要实现过期,您可以设置警报AlarmManager以唤醒BroadcastReceiver只会取消通知的警报.(如果通知不再存在,则取消的呼叫将不执行任何操作.)
// 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);
Run Code Online (Sandbox Code Playgroud)
在你的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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4660 次 |
| 最近记录: |