gpt4 book ai didi

ios - 如何更改每日通知中的文本?

转载 作者:行者123 更新时间:2023-11-29 01:39:29 25 4
gpt4 key购买 nike

我有一个每日通知,提醒用户使用该应用程序。我希望文本不是静态的。我说了 5 个不同的字符串,如果可能的话,我希望随机选择字符串。

我当前的代码,每天都使用相同的文本:

   let date: NSDate = NSDate()
let cal: NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!

let newDate: NSDate = cal.dateBySettingHour(22, minute: 00, second: 0, ofDate: date, options: NSCalendarOptions())!


var localNotification: UILocalNotification = UILocalNotification()
localNotification.alertAction = "MyApp"
localNotification.alertBody = "Call to action text"
localNotification.fireDate = newDate
localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

最佳答案

您可以根据星期几使用 switch 语句。这是 excellent NSHipster tutorial on NSDate components 。这是一些sample code回复:如何从 NSDate 中提取一周中的某一天:

func getDayOfWeek(today:String)->Int {

let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let todayDate = formatter.dateFromString(today)!
let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let myComponents = myCalendar.components(.CalendarUnitWeekday, fromDate: todayDate)
let weekDay = myComponents.weekday
return weekDay
}

let weekday = getDayOfWeek("2014-08-27")
println(weekday) // 4 = Wednesday

Here's Apple's documentation对于 switch 语句。

基本上,您需要创建一个方法,将 NSDate 作为输入并输出一周中的某一天。获得星期几后,您可以在 switch 语句中创建自定义消息。

关于ios - 如何更改每日通知中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32576550/

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