@
nyanyh 您的链接里,有一句:
NSUserNotificationCenter.default.delegate = self
会报错:
Cannot assign value of type 'MyController' to type 'NSUserNotificationCenterDelegate?'
不知道怎么解决?
我在网上 copy 了一段:
```
@
IBAction func notificationAction(_ sender: Any) {
let content = UNMutableNotificationContent()
content.title = "新的方式"
content.body = "我是一个新的方式"
content.userInfo = ["method": "new"]
content.sound = UNNotificationSound.default
content.categoryIdentifier = "NOTIFICATION_DEMO"
let acceptAction = UNNotificationAction(identifier: "SHOW_ACTION", title: "显示", options: .init(rawValue: 0))
let declineAction = UNNotificationAction(identifier: "CLOSE_ACTION", title: "关闭", options: .init(rawValue: 0))
let testCategory = UNNotificationCategory(identifier: "NOTIFICATION_DEMO",
actions: [acceptAction, declineAction],
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder: "",
options: .customDismissAction)
let request = UNNotificationRequest(identifier: "NOTIFICATION_DEMO_REQUEST",
content: content,
trigger: nil)
// Schedule the request with the system.
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.delegate = self
notificationCenter.setNotificationCategories([testCategory])
notificationCenter.add(request) { (error) in
if error != nil {
// Handle any errors.
}
}
}
```
里面的 notificationCenter.delegate = self 也报了同样的错,这个 self 请问应该如何解决呢。