為什么沒有為UNCalendarNotificationTrigger觸發本地通知 [英] Why local notification is not firing for UNCalendarNotificationTrigger

查看:0
本文介紹了為什么沒有為UNCalendarNotificationTrigger觸發本地通知的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

當地通知應該在2021年9月25日發出。下面是Fire Time的打印對象

?年:2021年月:9天:26 isLeapMonth:False

  • 年份:2021年
  • 月份:9
  • 天:25
  • isLeapMonth:False
let content = UNMutableNotificationContent()
content.title = "Test title"
content.body = "sample test body"
var trigger:UNCalendarNotificationTrigger
let n = 1
let nextTriggerDate = Calendar.current.date(byAdding: .day, value: n, to: Date())!
let comps = Calendar.current.dateComponents([.year, .month, .day], from: nextTriggerDate)
trigger = UNCalendarNotificationTrigger(dateMatching: comps, repeats: false)
content.subtitle = "Sub title-Date-NonRepeat"
let request = UNNotificationRequest(identifier: "test.my.example", content: content, trigger: trigger)    
UNUserNotificationCenter.current().add(request) { Error in                        
    if let err = Error {
        print("Notification Error:(String(describing: err))")
    }
}

我再次添加了帶有日期的時間,并進行了以下代碼更改

var comps = Calendar.current.dateComponents([.year, .month, .day], from: nextTriggerDate)
comps.day = 25
comps.hour = 12
comps.minute = 17
comps.second = 10

這是COMPS變量的PO

Var coms=Calendar.Current.DateComponents([.年、.月、.日], 發件人:下一次觸發日期) Comps.day=25 Comps.Hour=12 計算機分鐘=17 Comps.Second=10

我只是給出了日期,看看它是否會啟動,我只給出了日期和時間,看看它是否會啟動,我在主線程中做了所有的事情,但它不工作

有人能讓我明白我做錯了什么嗎

推薦答案

我已經檢查了您的代碼,很明顯您正在正確添加日歷觸發器,并且您在其中一條評論中提到您在添加時也獲得了必要的權限。

對我來說,這感覺像是約會時間的問題。檢查DateTime到DateTime組件的對話是否正確進行。

如果您在模擬器上進行測試,當您來回更改系統中的日期時,事情可能會變得很瘋狂。

我建議您使用真實設備進行測試。

這是您獲得權限的方式

  UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.announcement,.sound]) { [self]
                    (granted, error) in
                    if granted {
                 print("granted")
                    } else {
                 print("denied or error")
                    }
                }

您可以使用以下代碼查詢可用通知列表,以確定是否在預期的日期時間正確安排了通知。

var text:NSMutableAttributedString? = NSMutableAttributedString(string: "List of notification requests and it's time
")

UNUserNotificationCenter.current().getPendingNotificationRequests() { [weak self] requests in
                DispatchQueue.main.async {
                for request in requests {
                            guard let trigger = request.trigger as? UNCalendarNotificationTrigger else { return }
                            text?.append(NSAttributedString(string: "
Trigger Date:(trigger.nextTriggerDate()?.description) 
DateTime Component:(trigger.dateComponents.description)
"))
                        }
                print(text)
                    }

更新(以下Credit(lorem ipsum)來自問題中的answer)

func getFireDatetime(apiDate: Date, numberOfDaysBeforeApiDate: Int,repeat:Bool) -> DateComponents? {
    if let fireDate: Date = apiDate.dateByAdding(days: -numberOfDaysBeforeApiDate) {
        if repeat {
            return Calendar.current.dateComponents([.day, .hour, .minute, .second], from: fireDate)
        }
        return Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: fireDate)
    }
return nil
}

調用getFireDatetime方法,我想這應該可以解決您的所有問題

P.S

  • date()它按世界時返回時間,而DateTime組件將使用您設備的當前日歷設置提供DateTime。

  • 當您使用DateTime組件時,應自動處理時區更改和夏令時

這篇關于為什么沒有為UNCalendarNotificationTrigger觸發本地通知的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持IT屋!

查看全文
登錄 關閉
掃碼關注1秒登錄
發送“驗證碼”獲取 | 15天全站免登陸
全免费A级毛片免费看无码播放