簡體   English   中英

本地通知可在ios模擬器上運行,但不能在設備上運行

[英]Local notifications working on ios simulator but not on device

我一直在努力使本地通知在正在使用的此應用程序上正常運行。 我已經在標題,正文蝕刻中設置了帶有一些文本的通知內容,然后設置了UNCalendarNotificationTrigger

NSDate *tenSecondsFromNow = [cal dateByAddingUnit:NSCalendarUnitSecond value:10 toDate:[NSDate date] options:NSCalendarMatchFirst]
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger
                                          triggerWithDateMatchingComponents:[cal components:NSCalendarUnitYear |
                                                                             NSCalendarUnitMonth|
                                                                             NSCalendarUnitDay|
                                                                             NSCalendarUnitWeekday |
                                                                             NSCalendarUnitHour|
                                                                             NSCalendarUnitMinute|
                                                                                   NSCalendarUnitSecond fromDate:tenSecondsFromNow] repeats:NO];

並且我將通知安排為在運行此通知后的10秒鍾內運行

    // Schedule the notification.
[_notificationCentre addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    if (error != nil) {
        DDLogError(@"%@", error.localizedDescription);
        DDLogError(@"%@", @"couldn't create chore reminder notification");
    }
}];

我正在苦苦掙扎的是,當我在模擬器上運行此代碼時,通知完全符合我的預期,在前台或后台運行這段代碼后10秒鍾向我顯示了一條通知。 但是,當我在設備上運行此命令時,不會出現通知。 但是,當我將觸發器更改為此處的UNTimeIntervalNotificationTrigger設置時

UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];

本質上是在做同樣的事情,在addNotificationRequest函數被調用后的10秒鍾內調度通知。 這在模擬器和我的設備上均有效。

我想知道是否發生了一些奇怪的事情? 或是模擬器處理通知的方式與裝置略有不同。 無論哪種方式,我都不知道發生了什么。

我不完全確定為什么這樣做,但是減少了要在觸發器“修復它”中匹配的日期組件的數量。 我仍然不知道問題的根本原因,而是將數據組件部分更改為

[cal components:NSCalendarUnitHour|NSCalendarUnitMinute fromDate:tenSecondsFromNow]

解決了問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM