如何通过小部件 iOS14 将 UserNotification 处理到 swift 中的主应用程序?

How to handle UserNotification through widget iOS14 to main app in swift?

我有一个带有小部件 iOS 14 的 iOS 应用程序,我从两侧发送 UserNotifications。在主应用程序方面,所有 UNUserNotificationCenterDelegate 工作正常。在小部件方面,当我从日历中获取事件时 UserNotifications 被触发。当我在主应用程序端收到通知并单击它时,didReceive response: UNNotificationResponse 委托或其他委托没有调用,也没有在小部件端调用。

这是我在 Widgets 中的代码

class NotificationCenter: NSObject, ObservableObject {
    @Published var dumbData: UNNotificationResponse?
    
    override init() {
        super.init()
        UNUserNotificationCenter.current().delegate = self
    }
}
extension NotificationCenter: UNUserNotificationCenterDelegate  {
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert, .sound, .badge])
    }

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

    func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {
        
    }
    func scheduleNotification(at date: Date, identifierUnic : String, body: String, titles:String) {
        UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

        let triggerWeekly = Calendar.current.dateComponents([.day, .month, .hour,.minute, .year], from: date)
        let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
        let content = UNMutableNotificationContent()
        content.title = titles
        content.body = body
        content.sound = UNNotificationSound.default
        content.categoryIdentifier = "todoList2"
        
        let request = UNNotificationRequest(identifier: identifierUnic, content: content, trigger: trigger)
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().add(request) {(error) in
            if let error = error {
                print(" We had an error: \(error)")
            }}
    }
}
//and fire in 
@ObservedObject var notificationCenter: NotificationCenter

 func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
        eventManager.checkPermission { (eventArray) in
            print(eventArray)
            if eventArray.count > 0{
                for item in eventArray{
                    notificationCenter.scheduleNotification(at: item.startDate ?? Date(), identifierUnic: item.eventIdentifier ?? "", body: item.title, titles: item.title)
                }
            }
        }
        
        var entries: [SimpleEntry] = []
        // Generate a timeline consisting of five entries an hour apart, starting from the current date.
        let currentDate = Date()
        for hourOffset in 0 ..< 5 {
            let entryDate = Calendar.current.date(byAdding: .second, value: hourOffset, to: currentDate)!
            let entry = SimpleEntry(date: entryDate)
            entries.append(entry)
        }
        
        let timeline = Timeline(entries: entries, policy: .atEnd)
        completion(timeline)
    }

在主应用端

     func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
         let userInfo = response.notification.request.content.userInfo
        print("userInfo \(userInfo)")
}

请告诉我? 谢谢

我遇到了类似的情况,但我仍然不知道该怎么做...我有一个可以显示日历事件的小部件。日历更改时如何刷新小部件?我尝试使用 notificationCenter 但不知道该放在哪里。我想做的是当日历发生变化时,我的小部件应该重新加载(刷新时间线。)

感谢您的帮助。