gpt4 book ai didi

ios - UNCalendarNotificationTrigger 未启动

转载 作者:行者123 更新时间:2023-12-04 12:35:11 28 4
gpt4 key购买 nike

好的 - 我现在对这段代码感到非常沮丧,准备放弃!基本上,当模拟到模拟器或实际设备时,我得到 requestAuthorisation 可以正常工作,但触发器不会启动。我在网上关注了几个人,他们的代码运行起来很轻松!当我使用按钮启动 UNTimeIntervalNotificationTrigger 时,它可以工作,但这不是我想要的。目前在 iOS 14.3 中作为构建目标进行测试。应用程序的其余部分构建没问题。我究竟做错了什么?!忍不住想,在试图让它工作的某个地方,我可能已经损坏了 info.plist 或类似的东西?!我已经测试过重复触发和不重复,但都不起作用。

    override func viewDidLoad() {
super.viewDidLoad()

//NOTIFICATIONS
// Step 1 - Ask the use for permission to notify
let randVerseCenter = UNUserNotificationCenter.current()
randVerseCenter.requestAuthorization(options: [.alert, .sound]){ (granted, error) in
if granted {
print("Yay - request authorisation worked!")
} else {
print ("D'oH - request Authorisation did not work!")
}
}
// Step 2 - Create the Notification Content
let randVerseContent = UNMutableNotificationContent()
randVerseContent.title = "Random Reference"
randVerseContent.body = "Random Verse"
randVerseContent.sound = UNNotificationSound.default
// Step 3 - Create the trigger for the notification by delay
let randVerseDate = Date().addingTimeInterval(30)
let randVerseDateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: randVerseDate)
let randVerseTrigger = UNCalendarNotificationTrigger(dateMatching: randVerseDateComponents, repeats: true)
// Step 4 - Creating the request
let randVerseUUIDString = UUID().uuidString
let randVerseRequest = UNNotificationRequest(identifier: randVerseUUIDString, content: randVerseContent, trigger: randVerseTrigger)
// Step 5 - Register the request
randVerseCenter.add(randVerseRequest) { (error) in
if let error = error{
print (error.localizedDescription)
}
//Check the error parameter and handle any errors
}
}

最佳答案

了解更多详细信息后,我想我知道为什么您仍然看不到正在发送的通知。我在另一个答案中让它不要太长,但我会保留 my previous answer供引用。
也许您正在等待应用程序在前台的通知?我将引用文档的另一部分:

Scheduling and Handling Local Notifications
在有关当您的应用程序位于前台时处理通知的部分:

If a notification arrives while your app is in the foreground, you cansilence that notification or tell the system to continue to displaythe notification interface. The system silences notifications forforeground apps by default, delivering the notification’s datadirectly to your app...

因此,如果是这种情况,您必须为 UNUserNotificationCenter 实现一个委托(delegate)。
我建议你这样做,在 AppDelegate 上你为 UNUserNotificationCenter 分配委托(delegate),因为文档说它必须在应用程序完成启动之前完成:

// AppDelegate.swift
@main
class AppDelegate: UIResponder, UIApplicationDelegate {


func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}

// Rest of your code on AppDelegate...
}

extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// Here we actually handle the notification
print("Notification received with identifier \(notification.request.identifier)")
// So we call the completionHandler telling that the notification should display a banner and play the notification sound - this will happen while the app is in foreground
completionHandler([.banner, .sound])
}
}

在您处理通知授权和请求注册的 View Controller 上,您可以这样做:

class NotificationsViewController: UIViewController {

static let notificationAuthorizedNotification = NSNotification.Name(rawValue: "NotificationAuthorizedNotification")
let randVerseCenter = UNUserNotificationCenter.current()

override func viewDidLoad() {
super.viewDidLoad()

// We call this method when we know that the user granted permission, so we know we can then make notification requests
NotificationCenter.default.addObserver(self, selector: #selector(handleNotificationAuthorization), name: NotificationsViewController.notificationAuthorizedNotification, object: nil)

randVerseCenter.getNotificationSettings { [weak self] settings in
// We check current settings and asks for permission if not granted before
if settings.authorizationStatus == .notDetermined {
// Step 1 - Ask the use for permission to notify
self?.randVerseCenter.requestAuthorization(options: [.alert, .sound]){ (granted, error) in
if granted {
NotificationCenter.default.post(name: NotificationsViewController.notificationAuthorizedNotification, object: nil)
print("Yay - request authorisation worked!")
} else {
print ("D'oH - request Authorisation did not work!")
}
}
}
}
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// We stop listening to those notifications here
NotificationCenter.default.removeObserver(self)
}

@objc
func handleNotificationAuthorization() {
// Step 2 - Create the Notification Content
let randVerseContent = UNMutableNotificationContent()
randVerseContent.title = "Random Reference"
randVerseContent.body = "Random Verse"
randVerseContent.sound = UNNotificationSound.default
// Step 3 - Create the trigger for the notification by delay
let randVerseDate = Date().addingTimeInterval(30)
let randVerseDateComponents = Calendar.current.dateComponents([.second], from: randVerseDate)
let randVerseTrigger = UNCalendarNotificationTrigger(dateMatching: randVerseDateComponents, repeats: true)
// Step 4 - Creating the request
let randVerseUUIDString = UUID().uuidString
let randVerseRequest = UNNotificationRequest(identifier: randVerseUUIDString, content: randVerseContent, trigger: randVerseTrigger)
// Step 5 - Register the request
randVerseCenter.add(randVerseRequest) { (error) in
if let error = error{
print (error.localizedDescription)
} else {
print("Successfully registered notification with id \(randVerseUUIDString) at every second \(randVerseDateComponents.second!) of a minute")
}
}
}
}

您可能仍然安排了较早的通知,因为您的代码在 viewDidLoad 请求它们,并且您可能没有删除它们或删除应用程序。
您可以在您的 viewDidLoad 上使用它检查未决通知,例如:

        randVerseCenter.getPendingNotificationRequests() { requests in
for request in requests {
guard let trigger = request.trigger as? UNCalendarNotificationTrigger else { return }
print("Notification registered with id \(request.identifier) is schedulled for \(trigger.nextTriggerDate()?.description ?? "(not schedulled)")")
}
}

并使用 randVerseCenter 通过标识符删除它们或全部删除。

关于ios - UNCalendarNotificationTrigger 未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65601122/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com