如何快速通过小部件 iOS14 处理 UserNotification 到主应用程序?

问题描述

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

这是我在小部件中的代码

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,withCompletionHandler completionHandler: @escaping () -> Void) {
         let userInfo = response.notification.request.content.userInfo
        print("userInfo \(userInfo)")
}
  • 其中缺少什么?
  • 在两个目标上处理 UserNotification 是否正确?
  • 还有其他方法可以做到这一点吗?

请告诉我? 谢谢

解决方法

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

感谢您的帮助。