【问题标题】:How to schedule Notifications in Swift如何在 Swift 中安排通知
【发布时间】:2018-06-30 20:50:11
【问题描述】:

我正在尝试在我的项目中安排通知。我没有收到任何错误或运行时崩溃,但是由于某种原因,我在调度时没有在模拟器上收到任何通知。我不确定我到底哪里出错了,因为我在之前的项目中已经成功地做到了这一点。

有人有什么建议吗?

import UserNotifications

func addNotification(title: String, category: String, UI: String, date: Date) {

    // Remove Notification

    removeNotifcation(UI: UI)

    // Create Notification
    // iOS 10 Notification

    if #available(iOS 10.0, *) {

        let notif = UNMutableNotificationContent()

        notif.title = title
        notif.subtitle = "Reminder for \(title)"
        notif.body = "Your Reminder for \(title) set for \(date)"
        notif.sound = UNNotificationSound.default()
        notif.categoryIdentifier = UI

        let today = Date()

        let interval = date.timeIntervalSince(today as Date)

        let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: false)

        let request = UNNotificationRequest(identifier: title, content: notif, trigger: notifTrigger)

        UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
            if error != nil {
                print(error as Any)
                // completion(Success: false)
            } else {
                //completion(Sucess: true)
            }
        })
    } else {
        let newNotification:UILocalNotification = UILocalNotification()

        newNotification.category = category
        newNotification.userInfo = [ "UUID"  : UI]
        newNotification.alertBody = "Your reminder for \(title)"
        newNotification.fireDate = date
        //notification.repeatInterval = nil
        newNotification.soundName = UILocalNotificationDefaultSoundName

        UIApplication.shared.scheduleLocalNotification(newNotification)

    }

}

func removeNotifcation(UI: String) {

    //Remove Notif
    //iOS 10 Notification
    if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [UI])
    } else {
        //Remove Notif

        let app:UIApplication = UIApplication.shared
        for oneEvent in app.scheduledLocalNotifications! {
            let notification = oneEvent as UILocalNotification
            let userInfoCurrent = notification.userInfo!
            let uuid = userInfoCurrent["UUID"] as! String
            if uuid == UI {
                //Cancelling local notification
                app.cancelLocalNotification(notification)
                break;
            }
        }
    }

}

这就是我调用方法的方式

dataManager.addNotification(title: "String", category: "001", UI: uuid, date: datePicker.date)

【问题讨论】:

    标签: ios swift uilocalnotification unusernotificationcenter


    【解决方案1】:

    只有当你实现willPresent委托方法时,应用程序在前台时才不会出现通知,因为

     let today = Date()
    

    这个日期会在你运行它时触发它,所以添加一个时间间隔并将应用程序发送到后台你会看到它,并确保你在AppDelegate请求权限

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
            print("granted: (\(granted)")
        }
    
      return true
    }
    

    你也可以看看这个tutorial

    //

    编辑:以验证它无论如何都会触发实现此

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    
          print("handling notifications with the TestIdentifier Identifier")
    
         completionHandler()
    
    }
    

    并将委托设置为 VC

    UNUserNotificationCenter.current().delegate = self
    

    【讨论】:

    • 但是我确实使用了一个间隔,我使用传递给方法的日期设置间隔并设置从今天开始的间隔'让间隔 = date.timeIntervalSince(今天作为日期)''让 notifTrigger = UNTimeIntervalNotificationTrigger (timeInterval:间隔,重复:false)''让请求= UNNotificationRequest(标识符:标题,内容:通知,触发器:notifTrigger)'
    • 我也请求权限
    • 但是你没有在间隔中添加任何时间偏移,通过这个触发器 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) ,在应用启动后发送它到后台查看 5 秒结束前的通知
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    相关资源
    最近更新 更多