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

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

【问题讨论】:

标签: swift3 calendar localnotification


【解决方案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])
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      • 2016-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多