每5天快速重复LocalNotification [英] Swift Repeat LocalNotification every 5 days

查看:121
本文介绍了每5天快速重复LocalNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何每5天在上午10:00重复LocalNotification

How to repeat LocalNotification every 5 days at 10:00 AM

我尝试了一下,但是没有用

I try this, but it's not working

let content = UNMutableNotificationContent()
content.title = "Hello!"
content.body = "Hello_message_body"
content.sound = UNNotificationSound.default()

let futureTime = Date().addingTimeInterval(5 * 24 * 60 * 60)
var calendar = NSCalendar.current
calendar.timeZone = NSTimeZone.system
var components = calendar.dateComponents([.hour, .minute, .second], from: futureTime)
components.hour = 10
components.minute = 0
components.second = 0

let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
let request = UNNotificationRequest(identifier: "FiveDays", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request)

推荐答案

您可以使用UNCalendarNotificationTrigger获取本地通知,该通知在任何特定时间重复定义的几天或一周.

You can use UNCalendarNotificationTrigger to get Local Notification that repeats for any define days or week at any certain time.

let notificationContent = UNMutableNotificationContent()
notificationContent.title = "Hello!"
//notificationContent.subtitle = "Something"
notificationContent.body = "Hello_message_body"
notificationContent.sound = UNNotificationSound.default

// Add notification for Friday (after 5 days) at 10:00 AM
var dateComponents = DateComponents()
dateComponents.weekday = 5
dateComponents.hour = 10
dateComponents.minute = 0

let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "notification1", content: notificationContent, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

谢谢.

这篇关于每5天快速重复LocalNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆