繁体   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