gpt4 book ai didi

ios - 如何在swift 3中每天在特定时间触发本地通知

转载 作者:搜寻专家 更新时间:2023-11-01 06:04:46 25 4
gpt4 key购买 nike

我是 iOS 开发的新手,但已经创建了该应用程序,并且我正在尝试为特定时间(例如上午 10 点)创建每日通知。如果我将我的移动设备时间设置为上午 10 点,现在通知会太多。我只想在给定的特定时间(上午 10 点)触发一次本地通知,并且我想每天重复一次。每天重复通知的最佳方法是什么?

这是我的代码

func fire(){
let dateComp:NSDateComponents = NSDateComponents()
dateComp.hour = 10
dateComp.minute = 00
dateComp.timeZone = NSTimeZone.systemTimeZone()
let calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierIndian)!
let date:NSDate = calender.dateFromComponents(dateComp)!
let localNotificationSilent = UILocalNotification()
localNotificationSilent.fireDate = date
localNotificationSilent.repeatInterval = NSCalendarUnit.Day
localNotificationSilent.alertBody = "Started!"
localNotificationSilent.alertAction = "swipe to hear!"
localNotificationSilent.timeZone = NSCalendar.currentCalendar().timeZone
localNotificationSilent.category = "PLAY_CATEGORY"
UIApplication.sharedApplication().scheduleLocalNotification(localNotificationSilent)
}

最佳答案

在 Swift 3、iOS 10 中工作:

UNUserNotificationCenter.current().requestAuthorization(
options: [.alert, .sound, .badge], completionHandler: {userDidAllow, error in
//if userDidAllow : do something if you want to
})

//Set notification to trigger 11:30AM everyday
var dateComponents = DateComponents()
dateComponents.hour = 11
dateComponents.minute = 30
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

//Set your content
let content = UNMutableNotificationContent()
content.title = "Your notification title"
content.body = "Your notification body"

let request = UNNotificationRequest(
identifier: "yourIdentifier", content: content, trigger: trigger
)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

关于ios - 如何在swift 3中每天在特定时间触发本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40520704/

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