将本地通知设置为 swift 3 前 1 天或 2 天前

Posted

技术标签:

【中文标题】将本地通知设置为 swift 3 前 1 天或 2 天前【英文标题】:Set local notification for 1 day before or 2 day before swift 3 【发布时间】:2017-08-10 05:46:07 【问题描述】:

我想在我的应用中添加本地通知。我在textfields 中填写信息,并在为考试选择的特定日期之前设置datetime and reminder。任何人都实现了这样的演示然后请建议我该怎么做。

【问题讨论】:

观看此youtube.com/watch?v=CnvqLqoG4JM 【参考方案1】:

答案是根据我的理解,请根据您的要求更改时间和提醒字符串。

 func scheduleNotification(InputUser:String) 

let now: NSDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute], fromDate: NSDate())

let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let date = cal.dateBySettingHour(now.hour, minute: now.minute + 1, second: 0, ofDate: NSDate(), options: NSCalendarOptions())
let reminder = UILocalNotification()
reminder.fireDate = date
reminder.alertBody = InputUser
reminder.alertAction = "Cool"
reminder.soundName = "sound.aif"
reminder.repeatInterval = NSCalendarUnit.Minute




UIApplication.sharedApplication().scheduleLocalNotification(reminder)

print("Firing at \(now.hour):\(now.minute+1)")

【讨论】:

【参考方案2】:

在 1 天前设置每日基本本地通知,或者您可以在 Swift 3.1 中借助特定日期进行修改


import UIKit
import UserNotifications

fileprivate struct AlarmKey
    static let startWorkIdentifier = "com.Reminder.Notification" //Add your own Identifier for Local Notification
    static let startWork = "Ready for work? Toggle To \"Available\"."



class AlarmManager: NSObject

    static let sharedInstance = AlarmManager()

    override init() 
        super.init()
    

    //MARK: - Clear All Previous Notifications
    func clearAllNotifications()
        if #available(ios 10.0, *) 
            UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
         else 
            UIApplication.shared.cancelAllLocalNotifications()
        
    

    func addReminder(with _ hour: Int, minutes: Int)
        clearAllNotifications()

        var dateComponent = DateComponents()
        dateComponent.hour = hour // example - 7 Change you time here Progrmatically
        dateComponent.minute = minutes // example - 00 Change you time here Progrmatically


        if #available(iOS 10.0, *) 
            dateComponent.timeZone = TimeZone.autoupdatingCurrent

            let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: false) //Set here **Repeat** condition

            let content = UNMutableNotificationContent()
            content.body = AlarmKey.startWork //Message Body
            content.sound = UNNotificationSound.default()

            let notification = UNNotificationRequest(identifier: AlarmKey.startWorkIdentifier, content: content, trigger: trigger)
            UNUserNotificationCenter.current().delegate = self
            UNUserNotificationCenter.current().add(notification) (error) in
                if let error = error 
                    print("Uh oh! We had an error: \(error)")
                
            
         else 
            //iOS *9.4 below if fails the Above Condition....
            dateComponent.timeZone = NSTimeZone.system
            let calender = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
            let date = calender.date(from: dateComponent)!
            let localNotification = UILocalNotification()
            localNotification.fireDate = date
            localNotification.alertBody = AlarmKey.startWork
            localNotification.repeatInterval = NSCalendar.Unit.day
            localNotification.soundName = UILocalNotificationDefaultSoundName
            UIApplication.shared.scheduleLocalNotification(localNotification)
        
    
 

//This is optional method if you want to show your Notification foreground condition
extension AlarmManager: UNUserNotificationCenterDelegate
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void)
        completionHandler([.alert,.sound])
    

【讨论】:

以上是关于将本地通知设置为 swift 3 前 1 天或 2 天前的主要内容,如果未能解决你的问题,请参考以下文章

Swift:多个本地通知,但只显示最新的

Swift 3 本地通知 - 设置自动时区

如何在swift 3中每天在特定时间触发本地通知

设置特定日期和时间的本地通知 swift 3

iOS,Swift3)如何将通知声音设置为广播流?

用户选择的 swift 3 重复间隔中的本地通知