重新安排通知操作通知无法正常工作

编程入门 行业动态 更新时间:2024-10-18 16:55:32
本文介绍了重新安排通知操作通知无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当用户点击等待通知操作时,我正在尝试重新安排通知。当日期选择器首次提供 triggerNotification()时,则由日期选择器提供 Date ,但由响应调用时从时间间隔获取日期。在 didReceiveResponse 方法内部 case:waitActionIdentifier:我设置了 newDate 并将其传递给 triggerNotification 参数。延迟的通知永远不会到达。 我不知道这是否是函数调用问题,或者因为我看到的 newDate 印刷正确。 这是代码:

I'm trying to reschedule a notification when user tap on wait notification action. When triggerNotification() gets called the first time Dateis provided by a Date Picker, but when it's called by a response gets the date from a time interval. Inside the didReceiveResponsemethod case:waitActionIdentifier: I set newDate and pass that to the triggerNotificationparameter. The delayed notification never arrives. I don't understand if it's a function calling problem or else because newDatei see is correct by the print of it. Here's the code:

class ViewController: UIViewController { @IBAction func datePickerDidSelectNewDate(_ sender: UIDatePicker) { let selectedDate = sender.date let delegate = UIApplication.shared.delegate as? AppDelegate // delegate?.scheduleNotification(at: selectedDate) delegate?.triggerNotification(at: selectedDate) } }

和AppDelegate

and the AppDelegate

import UIKit import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. UNUserNotificationCenter.current().delegate = self configureCategory() triggerNotification(at: Date()) requestAuth() return true } let category = "Notification.Category.Read" private let readActionIdentifier = "Read" private let waitActionIdentifier = "Wait" private func requestAuth() { UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (success, error) in if let error = error { print("Request Authorization Failed (\(error), \(error.localizedDescription))") } } } func triggerNotification(at date: Date) { // Create Notification Content let notificationContent = UNMutableNotificationContent() //compone a new Date components because components directly from Date don't work let calendar = Calendar(identifier: .gregorian) let components = calendar.dateComponents(in: .current, from: date) let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute) //create the trigger with above new date components, with no repetitions let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false) // Configure Notification Content notificationContent.title = "Hello" notificationContent.body = "Kindly read this message." // Set Category Identifier notificationContent.categoryIdentifier = category // Add Trigger // let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 10.0, repeats: false) // // Create Notification Request let notificationRequest = UNNotificationRequest(identifier: "test_local_notification", content: notificationContent, trigger: trigger) // // Add Request to User Notification Center UNUserNotificationCenter.current().add(notificationRequest) { (error) in if let error = error { print("Unable to Add Notification Request (\(error), \(error.localizedDescription))") } } } private func configureCategory() { // Define Actions let read = UNNotificationAction(identifier: readActionIdentifier, title: "Read", options: []) let wait = UNNotificationAction(identifier: waitActionIdentifier, title : "Wait", options: []) // Define Category let readCategory = UNNotificationCategory(identifier: category, actions: [read, wait], intentIdentifiers: [], options: []) // Register Category UNUserNotificationCenter.current().setNotificationCategories([readCategory]) } func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { completionHandler([.alert]) } func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { switch response.actionIdentifier { case readActionIdentifier: print("Read tapped") case waitActionIdentifier: let actualDate = Date() let newDate: Date = Date(timeInterval: 10, since: actualDate) self.triggerNotification(at: newDate) print("Wait tapped") print(actualDate) print(newDate) default: print("Other Action") } completionHandler() } }

推荐答案

之后用@ VIP-DEV和@Honey尝试不同的东西,我终于明白了在通知中点击等待操作按钮时,重新安排通知会出现问题。 在 func triggerNotification(日期:Date)范围内我正在获取没有秒的日期组件 let newComponents = DateComponents(日历:日历) ,timeZone:.current,month:components.month,day:components.day,hour:components.hour,minute:components.minute),因此,使用 let newDate:Date = Date(timeInterval:5.0,from:ActualDate) date,当然,这些秒数没有考虑在内,因此使用的日期已经过去。无需重新安排。 let newComponents = DateComponents(calendar:calendar,timeZone:.current,month:components.month,day:components.day,hour:components.hour,min:components.minute,second: component.second)是包含秒在内的组件的最终日期。

After trying different thing with @VIP-DEV and @Honey I finally understood where the problem with rescheduling notification when tapping on the wait action button from the notification. Inside the func triggerNotification(at date: Date)scope I was getting the date components without seconds let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)so when the new call of the function was made with a let newDate: Date = Date(timeInterval: 5.0 , since: actualDate)date, of course those seconds weren't taken into account and the date used was already passed, hence..no rescheduling . let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute, second: components.second) is the final date with components including seconds.

更多推荐

重新安排通知操作通知无法正常工作

本文发布于:2023-07-14 10:00:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1104036.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知   无法正常   操作   工作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!