- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在迁移到 NUUserNotification,但我遇到了问题,因为我正在使用较新的框架学习通知,但我不知道较旧的框架。我希望这不是重复的,因为我阅读了很多有关如何迁移的帖子和教程,但没有找到让我了解如何应用于此案例的内容。我正在更新的代码不是我的,我花了一些时间来理解它。它是 iOS Alarm ( https://github.com/natsu1211/Alarm-ios-swift ) 的克隆,我将其合并到我的应用程序中,以在预定的时间/日期等执行操作...据我所知,我难以理解的两件事是通知重复现在理解是每个工作日还是单个时间,对吗? fireDate 参数已被折旧,那么我将如何重写它? :
private func minFireDateWithIndex(notifications: [UILocalNotification]) -> (Date, Int)? {
if notifications.isEmpty {
return nil
}
var minIndex = -1
var minDate: Date = notifications.first!.fireDate!
for n in notifications {
let index = n.userInfo!["index"] as! Int
if(n.fireDate! <= minDate) {
minDate = n.fireDate!
minIndex = index
}
}
return (minDate, minIndex)
}
我更新时遇到问题的另一个代码是检查现有通知,现在 getPendingNotificationRequestsWithCompletionHandler:]
。我认为我已经更新了定义通知的旧函数中的 if let n = UIApplication.shared.scheduledLocalNotifications 。旧代码:
func setupNotificationSettings() -> UIUserNotificationSettings {
var snoozeEnabled: Bool = false
if let n = UIApplication.shared.scheduledLocalNotifications {
if let result = minFireDateWithIndex(notifications: n) {
let i = result.1
snoozeEnabled = alarmModel.alarms[i].snoozeEnabled
}
}
// Specify the notification types.
let notificationTypes: UIUserNotificationType = [UIUserNotificationType.alert, UIUserNotificationType.sound]
// Specify the notification actions.
let stopAction = UIMutableUserNotificationAction()
stopAction.identifier = Id.stopIdentifier
stopAction.title = "OK"
stopAction.activationMode = UIUserNotificationActivationMode.background // choose activation mode for app on tapping notification
stopAction.isDestructive = false
stopAction.isAuthenticationRequired = false
let snoozeAction = UIMutableUserNotificationAction()
snoozeAction.identifier = Id.snoozeIdentifier
snoozeAction.title = "Snooze"
snoozeAction.activationMode = UIUserNotificationActivationMode.background
snoozeAction.isDestructive = false
snoozeAction.isAuthenticationRequired = false
let actionsArray = snoozeEnabled ? [UIUserNotificationAction](arrayLiteral: snoozeAction, stopAction) : [UIUserNotificationAction](arrayLiteral: stopAction)
let actionsArrayMinimal = snoozeEnabled ? [UIUserNotificationAction](arrayLiteral: snoozeAction, stopAction) : [UIUserNotificationAction](arrayLiteral: stopAction)
// Specify the category related to the above actions.
let alarmCategory = UIMutableUserNotificationCategory()
alarmCategory.identifier = "myAlarmCategory"
alarmCategory.setActions(actionsArray, for: .default)
alarmCategory.setActions(actionsArrayMinimal, for: .minimal)
let categoriesForSettings = Set(arrayLiteral: alarmCategory)
// Register the notification settings.
let newNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: categoriesForSettings)
UIApplication.shared.registerUserNotificationSettings(newNotificationSettings)
return newNotificationSettings
}
新代码编辑:
func setRouteNotification(_ date: Date, onWeekdaysForNotify weekdays:[Int], snoozeEnabled:Bool, onSnooze: Bool, soundName: String, routeName: String, index: Int) {
// Notification content
let routeCheckNotificationContent = UNMutableNotificationContent()
let datesForNotification = correctDate(date, onWeekdaysForNotify: weekdays)
routeCheckNotificationContent.title = "Hello!! Are you ready to cycle?"
routeCheckNotificationContent.body = "Check route for alerts?"
routeCheckNotificationContent.categoryIdentifier = Id.notificationCategory
routeCheckNotificationContent.sound = UNNotificationSound.init(named: soundName + ".mp3") // check for the + ".mp3"
// Define actions
let check = UNNotificationAction(identifier: Id.checkActionIdentifier, title: " Check", options: [.foreground])
let wait = UNNotificationAction(identifier: Id.waitActionIdentifier, title: "Wait", options: [])
// Define category
let routeCategory = UNNotificationCategory(identifier: Id.notificationCategory, actions: [check, wait], intentIdentifiers: [], options: [])
// Register category
UNUserNotificationCenter.current().setNotificationCategories([routeCategory])
let repeating: Bool = !weekdays.isEmpty
routeCheckNotificationContent.userInfo = ["snooze" : snoozeEnabled, "index": index, "soundName": soundName, "routeName": routeName, "repeating" : repeating]
//repeat weekly if repeat weekdays are selected
//no repeat with snooze notification
if !weekdays.isEmpty && !onSnooze{
}
syncAlarmModel()
for d in datesForNotification {
if onSnooze {
alarmModel.alarms[index].date = Scheduler.correctSecondComponent(date: alarmModel.alarms[index].date)
}
else {
alarmModel.alarms[index].date = d
}
// Notification trigger
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: d)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute, second: components.second, weekday: components.weekday)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: true)
// Notification Request
let routeNotificationRequest = UNNotificationRequest(identifier: "routeNotificationRequest", content: routeCheckNotificationContent, trigger: trigger)
// Add request
UNUserNotificationCenter.current().add(routeNotificationRequest) { (Error) in
if Error != nil {
print("something went wrong with adding notification")
}
}
}
}
var weekdays: [Int]!
是一个填充在 WeekdaysViewController(TableViewController)中的数组,选择行。
现在,当我选择任何一天时,通知都不会触发。
最佳答案
@Mikael。 @蜂蜜。在与代码进行了一些斗争之后,因为我认为我只能使用 .weekday 组件来设置重复,所以我发现我可以使用与使用 Date 设置每天重复通知相同的逻辑,因为我将所选日期存储到数组中,所以我确实设置了一个 for in 循环来为每个存储的日期设置一个具有唯一标识符的新通知。最终代码为:
func setRouteNotification(_ date: Date, onWeekdaysForNotify weekdays:[Int], snoozeEnabled:Bool, onSnooze: Bool, soundName: String, routeName: String, index: Int) {
// Notification content
let routeCheckNotificationContent = UNMutableNotificationContent()
let datesForNotification = correctDate(date, onWeekdaysForNotify: weekdays)
routeCheckNotificationContent.title = "Hello!! Are you ready to cycle?"
routeCheckNotificationContent.body = "Check route for alerts?"
routeCheckNotificationContent.categoryIdentifier = Id.notificationCategory
routeCheckNotificationContent.sound = UNNotificationSound.init(named: soundName + ".mp3") // check for the + ".mp3"
// Define actions
let check = UNNotificationAction(identifier: Id.checkActionIdentifier, title: " Check", options: [.foreground])
let wait = UNNotificationAction(identifier: Id.waitActionIdentifier, title: "Wait", options: [])
// Define category
let routeCategory = UNNotificationCategory(identifier: Id.notificationCategory, actions: [check, wait], intentIdentifiers: [], options: [])
// Register category
UNUserNotificationCenter.current().setNotificationCategories([routeCategory])
let repeating: Bool = !weekdays.isEmpty
routeCheckNotificationContent.userInfo = ["snooze" : snoozeEnabled, "index": index, "soundName": soundName, "routeName": routeName, "repeating" : repeating]
//repeat weekly if repeat weekdays are selected
//no repeat with snooze notification
if !weekdays.isEmpty && !onSnooze{
}
syncAlarmModel()
var counter = 0
for d in datesForNotification {
if onSnooze {
alarmModel.alarms[index].date = Scheduler.correctSecondComponent(date: alarmModel.alarms[index].date)
}
else {
alarmModel.alarms[index].date = d
}
// Notification trigger
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: d)
var newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute, second: components.second, weekday: components.weekday)
newComponents.weekday = weekdays[counter]
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: true)
// Notification Request
// let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
let routeNotificationRequest = UNNotificationRequest(identifier: "routeNotificationRequest\(counter)", content: routeCheckNotificationContent, trigger: trigger)
// Add request
UNUserNotificationCenter.current().add(routeNotificationRequest) { (Error) in
if Error != nil {
print("something went wrong with adding notification")
}
}
print("added request\(counter)")
counter = ( counter + 1 )
}
print(alarmModel.alarms)
}
关于ios - 快速从 UILocalNotification 迁移到 UNUserNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52252464/
我做了一个像这样的UNUserNotification: let content = UNMutableNotificationContent() content.title = "a
我在 iOS 10 中使用新的 UNUserNotification 框架。我可以看到如何添加操作按钮,但是当用户点击通知本身时我该如何响应?在我的例子中,它将是一张带有一些文字的图片。 默认行为是打
当我安排一个近期的日期(比如 1 小时后)时会显示通知,但当我安排明天或今晚晚些时候时,它们永远不会显示在我的真实数据上。关于为什么会发生这种情况的任何想法?我仔细检查了 trigger.nextTr
我试图让用户安排每天在特定时间打开应用程序的通知。到目前为止,我已经能够通过计算从现在到用户选择之间的时间来安排第一个通知,并在 X 秒内安排通知。但是,有没有一种方法可以将通知设置为每天重复?如果您
我正在如下设置多个 UNUsernotifications, - (void)viewDidLoad { [super viewDidLoad]; notifCount = 0;
我正在如下设置多个 UNUsernotifications, - (void)viewDidLoad { [super viewDidLoad]; notifCount = 0;
这是我的UNUserNotification代码 func scheduleNotification() { UNUserNotificationCenter.current
我想在应用程序进入后台后,每隔 60 秒发送一次通知。但是当你进入后台时,通知会立即发送,之后每60秒发送一次通知。我不想立即发送通知,我该怎么做? 这是我的代码, func application(
我正在迁移到 NUUserNotification,但我遇到了问题,因为我正在使用较新的框架学习通知,但我不知道较旧的框架。我希望这不是重复的,因为我阅读了很多有关如何迁移的帖子和教程,但没有找到让我
您好,我有一个音频播放器应用程序,其中有一个计时器部分。目标是使音频在特定日期和时间开始/停止。我已经使用 UNUserNotificationCenter 实现了此功能。当应用程序位于前台时,此功能
我已经设置了用户通知。他们交付得很好,但应用程序图标上的角标(Badge)始终是一个。这是我的代码: let center = UNUserNotificationCenter.current() l
我基于提醒应用程序和EKEvent构建了一个简单的待办事项应用程序。 我没有设置EKAlert的截止日期,而是构建了自己的UNUserNotification,这主要是因为我想拥有自定义操作事件。 该
我有一个包含用户“警报”的 iOS 应用程序 - 当应用程序不在前台时发送给用户。我正在使用 UNUserNotifications,在 iOS 10 和 iOS 11 测试中一切正常。 我还想覆盖仍
在代码中我尝试这样做: import UserNotifications if #available(iOS 10.0, *) { let center = UNUserNotific
我正在开发 Apple Watch 应用程序,它具有在每 2 或 3 或 5 分钟后向用户显示通知的功能。 我已经通过 iOS UNUserNotification 实现了这个功能,并且效果很好。 但
我想在标签中显示角标(Badge)编号的值。 到目前为止,我已将所有内容都放在我的 viewWillAppear 中。因此每次加载 Controller 时都会分配变量。这是代码: var deliv
所以我有一个交互式推送通知,但它突然停止工作,这是我的代码: func pushNotification(){ let content = UNMutableNotificationConte
我一直在构建余数应用程序,在特定时间设置余数,无论是否有重复模式。 我一直在努力解决一个问题。任何帮助都有助于解决问题。 这是我面临的场景 当在没有重复模式的情况下创建剩余部分并且我正在删除剩余部分时
我已经在这里工作了几个小时,据说我的一切都按预期运行,但它没有调用我的 userNotificationCenter:(UNUserNotificationCenter *)center willPr
我是一名优秀的程序员,十分优秀!