34 lines
1.1 KiB
Markdown
34 lines
1.1 KiB
Markdown
#ios
|
||
|
||
`getDeliveredNotificationsWithCompletionHandler:`是类中的一个方法`UNUserNotificationCenter`,允许您检索已发送给用户但仍在通知中心可见的通知。
|
||
|
||
### 用法
|
||
|
||
使用此方法的方法如下:
|
||
|
||
1. **导入用户通知框架**:
|
||
|
||
首先,确保文件顶部有必要的导入语句:
|
||
|
||
``` objc
|
||
@import UserNotifications;
|
||
```
|
||
|
||
2. **获取已发送的通知**:
|
||
|
||
您可以调用此方法来获取已发送通知的数组:
|
||
|
||
``` objc
|
||
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
||
[center getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
|
||
for (UNNotification *notification in notifications) {
|
||
NSLog(@"Delivered notification: %@", notification.request.content.title);
|
||
// You can access other properties like subtitle, body, etc.
|
||
}
|
||
}];
|
||
```
|
||
|
||
### 笔记
|
||
|
||
- 完成处理程序提供了一个对象数组`UNNotification`,每个对象代表一个已传递的通知。
|
||
- 此方法可用于检查已发送的通知以及可能删除它们或根据其内容执行操作。 |