如何在 swift 中的不同时间的特定星期几实施多个本地通知

How to implement multiple local notifications on specific day of weeks at different times in swift

我正在尝试在一周中的不同日子实施两个本地通知(1:星期三下午 6 点,2:星期日上午 10 点)我应用了很多东西,但我没有得到任何适当的解决方案。如果有人提供非常有帮助的解决方案,这是我应用的最后一个代码

import UserNotifications

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        let settings = UIUserNotificationSettings(types: [.alert, .sound, .badge], categories: nil)
        UIApplication.shared.registerUserNotificationSettings(settings)

        let localNotification1 = UILocalNotification()
        localNotification1.alertBody = "Stay up-to-date"
        localNotification1.alertTitle = "New content is now available. Watch now!"
        localNotification1.timeZone = NSTimeZone.default
        localNotification1.fireDate = self.getSunday() as Date?
//        localNotification1.soundName =
        UIApplication.shared.scheduleLocalNotification(localNotification1)

        let localNotification2 = UILocalNotification()
        localNotification2.alertBody = "New videos Uploaded"
        localNotification2.alertTitle = "Dear Doctor, Stay up-to-date. Watch now!"
        localNotification2.timeZone = NSTimeZone.default
        localNotification2.fireDate = self.getWednesday() as Date?
        UIApplication.shared.scheduleLocalNotification(localNotification2)

        return true
}

func getSunday() -> NSDate? {
        let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)
        let now: NSDate! = NSDate()

        let date10h = calendar.date(bySettingHour: 0, minute: 2, second: 0, of: now as Date, options: NSCalendar.Options.matchFirst)!

        return date10h as NSDate
    }

    func getWednesday() -> NSDate? {
        let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)
        let now: NSDate! = NSDate()

        let date19h = calendar.date(bySettingHour: 0, minute: 3, second: 0, of: now as Date, options: NSCalendar.Options.matchFirst)!
        return date19h as NSDate
    }

UILocalNotification 在 iOS 10 中已弃用。请改用 UNNotificationRequest

import UIKit
import UserNotifications

class ViewController: UIViewController, UNUserNotificationCenterDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //requesting for authorization
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in
                if error == nil {
                    self.addMultipleLocalNotification()
            }
        })
    }

    func addMultipleLocalNotification(){
        //creating the notification content
        let content = UNMutableNotificationContent()

        //adding title, subtitle, body and badge
        content.title = "Notification title "
        content.subtitle = "Notification sub-title "
        content.body = "We are learning about iOS Local Notification"
        content.badge = 1

        var sundayDate = DateComponents()
        sundayDate.weekday = 0 // which is Sunday
        sundayDate.hour = 10
        sundayDate.minute = 00

        var wedDate = DateComponents()
        wedDate.weekday = 3 // which is wednesday
        wedDate.hour = 18
        wedDate.minute = 00

        let trigger = UNCalendarNotificationTrigger(dateMatching: sundayDate, repeats: true)
         let trigger1 = UNCalendarNotificationTrigger(dateMatching: wedDate, repeats: true)
        let request = UNNotificationRequest(identifier: "notificationName", content: content, trigger: trigger)
        let request2 = UNNotificationRequest(identifier: "notificationName2", content: content, trigger: trigger1)


        UNUserNotificationCenter.current().delegate = self

        //adding the notification to notification center
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
           UNUserNotificationCenter.current().add(request2, withCompletionHandler: nil)

        UNUserNotificationCenter.current().getPendingNotificationRequests(
        completionHandler: { (notficiations) in
            for localNotification in notficiations {
                print(localNotification)
            }
        })
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        //displaying the ios local notification when app is in foreground
        completionHandler([.alert, .badge, .sound])
    }
}