[英]Swift 4 Repeating Local Notifications
I am trying it make a notification that will trigger every day at the same time (7:00AM). 我正在尝试发出一条将在每天的同一时间(7:00 AM)触发的通知。 I am able to get the notification to trigger if I use UNTimeIntervalNotificationTrigger
but not UNCalendarNotificationTrigger
which is what I really want. 如果我使用UNTimeIntervalNotificationTrigger
而不是UNCalendarNotificationTrigger
,那我就能得到要触发的通知,这正是我真正想要的。 This is what I have currently: 这是我目前拥有的:
In AppDelegate.swift I have: 在AppDelegate.swift中,我有:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (didAllow, error) in
if error != nil {
print(error as Any)
}
}
return true
}
And I remembered to import UserNotifications
too 我记得也要导入UserNotifications
When the user presses a button in ViewController.swift this is run: 当用户按下ViewController.swift中的按钮时,将运行该命令:
let content = UNMutableNotificationContent()
content.title = "Do your daily review"
content.badge = 1
let triggerTime = DateComponents.init(hour: 7, minute: 0, second: 0)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerTime, repeats: true)
let request = UNNotificationRequest(identifier: "dailyNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil
Could you please explain to me what is wrong here, and why UNTimeIntervalNotificationTrigger
works but UNCalendarNotificationTrigger
doesn't? 您能否向我解释一下这里出了什么问题,为什么UNTimeIntervalNotificationTrigger
可以工作,而UNCalendarNotificationTrigger
却不起作用?
I am not sure if it will fix your issue but you are setting a DateComponent directly from an init. 我不确定它是否可以解决您的问题,但您是直接从init设置DateComponent。 I would propose you to do it by initializing an empty DateComponent and only set the hours as you want. 我建议您通过初始化一个空的DateComponent并仅根据需要设置小时来做到这一点。
So, could you try to do this : 因此,您可以尝试执行以下操作:
let content = UNMutableNotificationContent()
content.title = "Do your daily review"
content.badge = 1
var triggerTime = DateComponents()
triggerTime.hour = 7
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerTime, repeats: true)
let request = UNNotificationRequest(identifier: "dailyNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
I did it from my own side and it works. 我从自己的角度做到了,并且有效。
I hope it may help. 希望对您有所帮助。
My original code works fine, I just needed to do my testing differently. 我的原始代码运行良好,只需要以不同的方式进行测试即可。 For some reason, if I set the time in my code to 7:00AM and then simulate that time on my Mac, it doesn't work, but if the time that I set it to is the real time of the day + 1 min, and I wait a real minute without changing my computer's time, it works. 出于某种原因,如果我将代码中的时间设置为7:00 AM,然后在Mac上模拟该时间,则该时间不起作用,但是如果我将其设置为实时时间+ 1分钟,并且我等待了一分钟,却没有改变计算机的时间,它可以正常工作。 Perhaps Apple stops notifications that come soon after the time jumps to stop people from having notification troubles when they change timezones. 也许Apple会在时间跳变后立即停止通知,以防止人们在更改时区时遇到通知问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.