首页 > 解决方案 > 有没有办法让小部件捕获 UNUserNotificationCenterDelegate?迅速

问题描述

请帮忙

在此处输入图像描述

用户通知

Import UserNotifications

把它放在 viewDidLoad

func setupNotif() {
      UNUserNotificationCenter.current().requestAuthorization(options [.alert,.sound]) {
        (granted, error) in
      }

      let notificationContent = UNMutableNotificationContent()
      notificationContent.title = "Push Invitation"
      notificationContent.subtitle = "Push subtitle"
      notificationContent.body = "Please check notif"
      notificationContent.sound = .default
      notificationContent.categoryIdentifier = categoryIdentifier

      let notificationTrigger = UNTimeIntervalNotificationTrigger.init(timeInterval: timeInterval, repeats: false)
        
      let notificationRequest = UNNotificationRequest.init(identifier: "SampleNotif", content: notificationContent, trigger: notificationTrigger)
      UNUserNotificationCenter.current().removeAllPendingNotificationRequests() 
      UNUserNotificationCenter.current().add(notificationRequest) {(error) in if (error != nil) {} else { }}
 }

这是 UUNotification 中心的代表

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent  notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
 }
 
 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
 }

标签: swiftpush-notificationwidget

解决方案


**我的回答,没有办法做到这一点..根据苹果..我认为这个问题将来可能会出现..

@available(iOS 10.0, *) 开放类 UNUserNotificationCenter : NSObject {

// The delegate can only be set from an application
weak open var delegate: UNUserNotificationCenterDelegate?

推荐阅读