2016-07-14 84 views
6

UILocalNotification已经大幅贬值,我想更新我的代码到UserNotification框架:UserNotification 3天,然后每天重复/小时 - iOS的10

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

相关? [如何在iOS 10用户通知上设置NSCalendarUnitMinute repeatInterval?](http://stackoverflow.com/q/37804287/2415822) – JAL

+0

相关,但似乎没有来自UserNotifications支持的折旧UILocalNotifications的.repeatInterval。寻找如何在新框架中处理这个问题,这样我就可以将我的代码从折旧的代码中移出。 –

+0

建议您提高雷达 – Wain

回答

2

看起来这是不支持的,但做一个变通方法,您可以使用:

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) 

我知道这ISN不是最优的,但它应该工作而不使用已弃用的类/方法。您使用重复:false,因为您在用户接收通知并创建新通知之前拦截通知。另外,如果您处理多个通知,则可以将它与UNNotificationAction和UNNotificationCategory结合使用。

+1

等一下...问题是询问本地通知,而不是远程通知,这是通知服务扩展的用途。你不能在本地调度的UNNotification上使用它们,对吗? – CMash

+0

实际上,您需要在远程通知中设置“可变内容”,才能使其通过服务扩展来处理,因此并非所有通知都会通过它。 – CMash

0

这应该工作:

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 
     } 
    }) 
}