简体   繁体   English

本地通知可在ios模拟器上运行,但不能在设备上运行

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

I've been working on getting local notifications working on this app I'm working on. 我一直在努力使本地通知在正在使用的此应用程序上正常运行。 I've setup the notification content with some text in the title, body etch and then setup the UNCalendarNotificationTrigger 我已经在标题,正文蚀刻中设置了带有一些文本的通知内容,然后设置了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];

and I schedule the notification to run 10 seconds after I run this 并且我将通知安排为在运行此通知后的10秒钟内运行

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

What I'm struggling with is that when I run this code on the simulator, the notification does exactly what I would expect, shows me a notification 10 seconds after I run this piece of code when in the foreground or the background. 我正在苦苦挣扎的是,当我在模拟器上运行此代码时,通知完全符合我的预期,在前台或后台运行这段代码后10秒钟向我显示了一条通知。 However when I run this on my device, the notification doesn't appear. 但是,当我在设备上运行此命令时,不会出现通知。 However when I change the trigger to be a UNTimeIntervalNotificationTrigger setup here 但是,当我将触发器更改为此处的UNTimeIntervalNotificationTrigger设置时

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

Which is essentially doing the same thing, scheduling a notification 10 seconds after the addNotificationRequest function is called. 本质上是在做同样的事情,在addNotificationRequest函数被调用后的10秒钟内调度通知。 This works on both the simulator and my device. 这在模拟器和我的设备上均有效。

I'm wondering if there is some weird time stuff going on? 我想知道是否发生了一些奇怪的事情? or maybe the simulator handles notifications slightly differently than the device. 或是模拟器处理通知的方式与装置略有不同。 Either way, I've got no idea what's going on. 无论哪种方式,我都不知道发生了什么。

I'm not exactly sure why this works, but reducing the number of date components to match in the trigger "fixed it". 我不完全确定为什么这样做,但是减少了要在触发器“修复它”中匹配的日期组件的数量。 I still don't know the root cause of the problem, but changing the data component part to 我仍然不知道问题的根本原因,而是将数据组件部分更改为

[cal components:NSCalendarUnitHour|NSCalendarUnitMinute fromDate:tenSecondsFromNow]

Fixed the problem. 解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM