Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift3 将本地通知设置为swift 3之前1天或之前2天_Swift3_Calendar_Localnotification - Fatal编程技术网

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

Swift3 将本地通知设置为swift 3之前1天或之前2天,swift3,calendar,localnotification,Swift3,Calendar,Localnotification,我想在我的应用程序中添加本地通知。我在文本字段中填写信息,并在为考试选择的特定日期之前设置日期、时间和提醒。如果有人实施了这样的演示,请建议我怎么做。答案基于我的理解,请根据您的要求更改时间和提醒字符串 func scheduleNotification(InputUser:String) { let now: NSDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute], fromDate: NSDa

我想在我的应用程序中添加本地通知。我在
文本字段中填写信息
,并在为考试选择的特定日期之前设置
日期
时间和提醒。如果有人实施了这样的演示,请建议我怎么做。

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

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

}

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