本文介绍了UserNotification 在 3 天内然后每天/每小时重复 - iOS 10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UILocalNotification 已被弃用,因此我想将我的代码更新到 UserNotification 框架:

UILocalNotification has been depreciated so I would like to update my code to the UserNotification framework:

let alertDays = 3.0
let alertSeconds = alertDays * 24.0 * 60.0 * 60.0

let localNotification:UILocalNotification = UILocalNotification()

localNotification.alertAction = "Reminder"
localNotification.alertTitle = "Reminder Title"
localNotification.alertBody = "Reminder Message"
localNotification.fireDate = Foundation.Date(timeIntervalSinceNow: alertSeconds)
localNotification.repeatInterval = .day
UIApplication.shared().scheduleLocalNotification(localNotification)

在等待初始通知后,如何使用 UserNotification 框架设置类似的每日或每小时重复次数?

How can I set a similar daily or hourly repeat with the UserNotification framework after waiting for the initial notification?

let alertDays = 3.0
let alertSeconds = alertDays * 24.0 * 60.0 * 60.0

let content: UNMutableNotificationContent = UNMutableNotificationContent()

content.title = "Reminder Title"
content.subtitle = "Reminder Subtitle"
content.body = "Reminder Message"

let calendar = Calendar.current

let alarmTime = Foundation.Date(timeIntervalSinceNow: alertSeconds)
let alarmTimeComponents = calendar.components([.day, .hour, .minute], from: alarmTime)

let trigger = UNCalendarNotificationTrigger(dateMatching: alarmTimeComponents, repeats: true)

let request = UNNotificationRequest(identifier: workoutAlarmIdentifier,
                                        content: content,
                                        trigger: trigger)

UNUserNotificationCenter.current().add(request)
    {
        (error) in // ...
    }

推荐答案

这似乎不受支持,但您可以使用以下解决方法:

It seems like this is not supported, but to make a workaround you could use:

let alertDays = 3.0
let daySeconds = 86400
let alertSeconds = alertDays * daySeconds

let content: UNMutableNotificationContent = UNMutableNotificationContent()

content.title = "Reminder Title"
content.subtitle = "Reminder Subtitle"
content.body = "Reminder Message"

let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: (alertSeconds), repeats: false)

let request = UNNotificationRequest(identifier: workoutAlarmIdentifier,
                                    content: content,
                                    trigger: trigger)

UNUserNotificationCenter.current().add(request)
{
    (error) in // ...
}

结合 didReceive(_:withContentHandler:),您可以使用:

let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: (daySeconds), repeats: false)

我知道这不是最佳选择,但它应该可以在不使用不推荐使用的类/方法的情况下工作.您使用 repeats: false ,因为您是在用户收到通知之前拦截通知并创建新通知.此外,如果您处理多个通知,您可以将其与 UNNotificationAction 和 UNNotificationCategory 结合使用.

I know this isn't optimal but it should work without using deprecated classes/methods. You use repeats: false since you are intercepting the notification just before the user receives it and creating a new notification. Additionally you can use it in combination with UNNotificationAction and UNNotificationCategory if you handle multiple notifications.

这篇关于UserNotification 在 3 天内然后每天/每小时重复 - iOS 10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 00:26