18

解决方案代码:

let center = UNUserNotificationCenter.current()
print(center.getPendingNotificationRequests(completionHandler: { error in
        // error handling here
    }))

我的原帖:

我正在尝试通过 UNUserNotificationCenter 获取UIApplication.shared.scheduledLocalNotifications已折旧的待处理通知列表。

这是我正在使用的代码:

let center = UNUserNotificationCenter.current()
    print(UNUserNotificationCenter.getPendingNotificationRequests(center))

但是,这会打印“(功能)”。getPendingNotificationRequests 需要一个 UNUserNotificationCenter 参数,我想不出它还能是什么。

谢谢

4

4 回答 4

41

getPendingNotificationRequests调用将一组请求传递给完成闭包。尝试这样的事情:

let center = UNUserNotificationCenter.current()
center.getPendingNotificationRequests(completionHandler: { requests in
    for request in requests {
        print(request)
    }
})
于 2016-10-29T04:57:00.960 回答
4

万一有人需要访问已经发送的通知(用户可以在通知中心看到它们),可以通过以下方式完成:

    let center = UNUserNotificationCenter.current()
    center.getDeliveredNotifications { notifications in
            // use center.removeDeliveredNotifications(withIdentifiers:requestIdsToRemove)
            // if you want to cancel some of the notifications displayed to user
        }
    }
于 2020-10-06T19:10:24.020 回答
1

实际上不是现在,但您应该允许使用通知

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
        if !granted {
            print("user has declined notifications")
        }
    }
于 2020-03-03T18:20:36.903 回答
0

对于 ObjC:- [[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest*> *requests){ NSLog(@"requests: %@", requests); }];

于 2021-03-21T17:50:02.190 回答