有人知道如何通过id获取通知吗?我的意思是,当我收到新通知时,如果它仍在Android状态栏中显示,我想获取相关信息并将其添加到新的通知中。谢谢。
有人知道如何通过id获取通知吗?我的意思是,当我收到新通知时,如果它仍在Android状态栏中显示,我想获取相关信息并将其添加到新的通知中。谢谢。
NotificationManager没有提供通过ID查找现有通知的方法。如果您想要更新通知,请发布一个新的通知,但使用相同的ID。它将显示为新的通知,或者更新具有该ID的现有通知。
你可以从NotificationManager获取活动通知列表。
@RequiresApi(api = Build.VERSION_CODES.M)
public Notification getActiveNotification(int notificationId) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
StatusBarNotification[] barNotifications = notificationManager.getActiveNotifications();
for(StatusBarNotification notification: barNotifications) {
if (notification.getId() == notificationId) {
return notification.getNotification();
}
}
return null;
}
fun getActiveNotification(context: Context, notificationId: Int): Notification? {
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val barNotifications = notificationManager.activeNotifications
return barNotifications.firstOrNull { it.id == notificationId }?.notification
}