iOS 10 如何使用UNUserNotificationCenter查看待处理通知列表?

iOS 10 How to view a list of pending notifications using UNUserNotificationCenter?

解决方案代码:

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

我原来的post:

我正在尝试通过 UNUserNotificationCenter 获取待处理通知列表,因为 UIApplication.shared.scheduledLocalNotifications 已贬值。

这是我正在使用的代码:

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

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

谢谢

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

let center = UNUserNotificationCenter.current()
center.getPendingNotificationRequests(completionHandler: { requests in
    for request in requests {
        print(request)
    }
})

现在还没有,但你应该允许使用通知

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
        if !granted {
            print("user has declined notifications")
        }
    }

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

    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
        }
    }

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