【问题标题】:Notification willPresent won't calling when app is closed应用程序关闭时通知 willPresent 不会调用
【发布时间】:2019-03-02 19:10:32
【问题描述】:

我试图仅在工作日通知用户,并且不想使用 for 循环执行此操作,因此通知将 bool 重复为 true。当应用程序打开时,willPresent 函数可以工作,但是当应用程序进入后台时,willPresent 函数不会被应用程序调用并在周末向用户发送通知,这是我不想要的。

我已经在 didFinishLaunchingWithOptions 设置了UNUserNotificationCenter.current().delegate = self

这是我的 AppDelegate 中的 willPresent 函数:

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

        let date = Date()
        var dayOfWeek = date.getWeekDay()
        if dayOfWeek.rawValue == 1 || dayOfWeek.rawValue == 7{
            print("Don't send a notification")
            completionHandler([])
        }else{
            completionHandler([.alert, .sound, .badge])
        }
    }

我正在使用的扩展:

extension Date {

    enum WeekDay: Int {
        case sunday = 1
        case monday
        case tuesday
        case wednesday
        case thursday
        case friday
        case saturday
    }

    func getWeekDay() -> WeekDay {
        let calendar = Calendar.current
        let weekDay = calendar.component(Calendar.Component.weekday, from: self)
        return WeekDay(rawValue: weekDay)!
    }
}

任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: swift notifications uilocalnotification localnotification


    【解决方案1】:

    那是因为你应该使用 didReceive 方法来接收后台通知

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      相关资源
      最近更新 更多