【问题标题】:UserNotification in 3 days then repeat every day/hour - iOS 10UserNotification 在 3 天内然后每天/每小时重复 - iOS 10
【发布时间】:2016-11-17 18:13:21
【问题描述】:

UILocalNotification 已被贬值,所以我想将我的代码更新为 UserNotification 框架:

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 框架设置类似的每日或每小时重复?

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 // ...
    }

【问题讨论】:

标签: swift date notifications repeat ios10


【解决方案1】:

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

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 结合使用。

【讨论】:

  • 等一下...问题是关于本地通知,而不是远程通知,这就是通知服务扩展的用途。你不能在本地预定的UNNotifications 上使用它们,对吧?
  • 事实上,您似乎需要在远程通知中设置“可变内容”以使其由服务扩展处理,因此并非所有通知都会通过它。
【解决方案2】:

这应该可行:

func addNotificationForAlarm(alarm: MyAlarm) {

    let myAlarmNotifContent = UNMutableNotificationContent()
    myAlarmNotifContent.title = "Reminder"
    myAlarmNotifContent.body = alarm.activity
    myAlarmNotifContent.sound = UNNotificationSound.default()

    if alarm.repeatDays.count == 1 {

    } else {

        for index in 1...alarm.repeatDays.count {
            createNotif(date: alarm.date, weekDay: index, content: myAlarmNotifContent)
        }
    }

}

private func createNotif(date: Date, weekDay: Int, content: UNNotificationContent) {

    var dateComponent = DateComponents()
    dateComponent.weekday = weekDay
    dateComponent.hour = Calendar.current.dateComponents([.hour], from: date).hashValue
    dateComponent.minute = Calendar.current.dateComponents([.minute], from: date).hashValue

    let myAlarmTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: true)

    setupNotificationSettings()

    let identifier = "Your-Notification"
    let request = UNNotificationRequest(identifier: identifier, content: content, trigger: myAlarmTrigger)
    let center = UNUserNotificationCenter.current()
    center.add(request, withCompletionHandler: { (error) in
        if error != nil {
            //TODO: Handle the error
        }
    })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2020-07-21
    • 1970-01-01
    • 2018-10-06
    • 2020-10-10
    • 2012-02-24
    相关资源
    最近更新 更多