gpt4 book ai didi

swift - 无法在 Swift 2 中安排本地通知

转载 作者:行者123 更新时间:2023-11-28 11:14:36 25 4
gpt4 key购买 nike

我最近更新到 Xcode 7 beta,我在安排 UILocalNotifications 时遇到了问题。如果有人能弄清楚为什么它们不起作用,我将非常感谢您的帮助。不用担心时间和日期等其他类,这些类是不言自明的,我已经验证它们可以正常工作。

我的目标:

class NotificationWithTime {
var UINotification = UILocalNotification()
init(title: String, day: Date, time: Time) {
let formatter = NSDateFormatter()
let dayday = day.day
let dayMonth = day.month
let dayYear = day.year
let timeMinute = time.minute
var timeHour = time.hour
if time.tod == TOD.PM {
timeHour! += 12
}
formatter.dateFormat = "yyyy-MM-dd HH:mm"
formatter.timeZone = NSTimeZone(abbreviation: "EST")
UINotification.alertAction = title
UINotification.fireDate = formatter.dateFromString("\(dayYear)-\(dayMonth)-\(dayday) \(timeHour!):\(timeMinute!)")
UIApplication.sharedApplication().scheduleLocalNotification(UINotification)
//print("set a new notification for" + "\(dayMonth)-\(dayday)-\(dayYear) \(timeHour):\(timeMinute)")
}
}

我的实例:

let time = ScheduleGenerator.refineTime(Time(t: TOD.PM, d: 20, h: 8, m: 12, s: 0))
_ = NotificationWithTime(title: "chicke is good", day: ScheduleGenerator.refineDate(Date(m: 9, d: 19, y: 2015)), time: time)

最佳答案

在 iOS 8 及更高版本中,使用本地或远程通知的应用必须注册它们打算传送的通知类型。

swift

    let mySettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil);

UIApplication.sharedApplication().registerUserNotificationSettings( mySettings);

目标-C

UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

关于swift - 无法在 Swift 2 中安排本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32679511/

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