【问题标题】:Local Notification Firing Multiple Times And At Unscheduled Hours本地通知在计划外的时间多次触发
【发布时间】:2023-03-21 12:29:01
【问题描述】:

我设置了一个本地通知,它应该在每天早上 8 点触发,但它确实会在早上 8 点触发 3 次,然后在上午 9 点和下午 12 点再次触发,之后它会停止并在下一次重复同样的事情天。

我检查了该函数是否被多次调用(不是),即使它被多次调用也不能解释在错误时间触发的原因。几乎所有我能想到的,重新实现了代码,但无济于事。

这就是我设置通知的方式:

func setUpMainLocalNotification() {

             let notificationSettings = UIApplication.sharedApplication().currentUserNotificationSettings()

                    if notificationSettings?.types == .None {

                        return

                    }


     let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
     let currentCalendar = NSCalendar.currentCalendar()
     let date = NSDate()
     let calendarComponents = calendar.components([.Year, .Month, .WeekOfMonth, .Hour, .Minute, .Second], fromDate: date)



                    calendarComponents.hour = 8

                    calendarComponents.minute = 0
                    calendarComponents.second = 0

                    currentCalendar.timeZone = NSTimeZone.defaultTimeZone()



                    let reminder = UILocalNotification()
                    reminder.timeZone = NSTimeZone.defaultTimeZone()
                    reminder.fireDate = currentCalendar.dateFromComponents(calendarComponents)
                    reminder.alertBody = "Notification"
                    reminder.category = "CATEGORY_ID"

                    reminder.repeatInterval = .Day

                    reminder.alertAction = "Reply"
                    reminder.soundName = "sound.aif" 



                    UIApplication.sharedApplication().scheduleLocalNotification(reminder)

}

更新:

当我在这里阅读我自己的问题时,我注意到我在设置我希望我的通知触发的时间之后设置时区,所以我在设置时间之前将以下行移到它似乎工作,触发只有一次,早上 8 点。

currentCalendar.timeZone = NSTimeZone.defaultTimeZone()

【问题讨论】:

    标签: swift uilocalnotification


    【解决方案1】:

    在设置新通知之前,您会在任何地方取消之前的通知吗?

    在你的方法开始时试试这个

    UIApplication.sharedApplication().cancelAllLocalNotifications()
    

    看看有没有帮助。

    【讨论】:

    • 谢谢,虽然我确实处理通知取消。我已经用解决方案更新了我的问题。
    【解决方案2】:

    这是最新的代码

    UIApplication.shared.cancelAllLocalNotifications()
    

    【讨论】:

      猜你喜欢
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多