菜鸟教程小白 发表于 2022-12-13 17:10:02

ios - UILocationNotfication.fireDate 过去 - 错误?


                                            <p><p>根据<a href="http://developer.apple.com/library/ios/#documentation/iphone/Reference/UILocalNotification_Class/Reference/Reference.html" rel="noreferrer noopener nofollow">Apple docs</a>关于<code>UILocationNotification.fireDate</code>:</p>

<blockquote>
<p>If the specified value is nil or is a date in the past, the
notification is delivered immediately.</p>
</blockquote>

<p>我在使用过去的日期时没有看到这种行为。是只有我,还是其他人也看到了?</p>

<p>这是我的代码:</p>

<pre><code>NSMutableArray *notifications = ;
UILocalNotification* alarm = [ init];
alarm.fireDate = ;
alarm.repeatInterval = 0;
alarm.soundName = @&#34;alarm.caf&#34;;
alarm.alertBody = @&#34;Test&#34;;
alarm.alertAction = @&#34;Launch&#34;;
NSMutableDictionary *userInfo = [ init];
forKey:@&#34;PsID&#34;];
alarm.userInfo = userInfo;
notifications = ;
UIApplication *app = ;
app.scheduledLocalNotifications = notifications;
</code></pre>

<p>如果我将 time(NULL)-5 更改为 time(NULL)+5,我会在此代码运行 5 秒后收到通知。使用 -5 值,我永远不会收到通知。</p>

<p>我知道这里的好问题需要有一个可能的明确答案,这可能会受到很多“我也是”答案的影响——<strong>所以我正在寻找的是来自官方(引用/链接)的东西Apple 表示这是预期行为,或者上述代码的不同版本按文档所说的那样工作。</strong></p>

<p>这对我的应用程序很重要,因为在某些情况下我需要通知用户警报,即使它发生在当天早些时候。我想我可以修改我的代码来检查当前时间并总是给出一个超出几秒的值——但我不确定“超出多少秒”是否真的安全,我希望它尽快发生——如果有更好的方法来获得“记录在案的行为”,也宁愿没有那个黑客。我的真实代码与上面类似,但我发布了几个通知,有些可能是过去的,有些可能是今天晚些时候,有些是明天或以后(这是用于日历应用程序)。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>@eselk,</p>

<p>我看到了和你一样的行为:一个新创建的 <code>UILocalNotification</code> 过去有一个 <strong>fireDate</strong> 不会触发 <strong><em>如果它已安装通过在 UIApplication 对象上设置 <code>scheduledLocalNotifications</code> 属性</em></strong>。</p>

<p>但是,如果使用 UIApplication 的 <code>scheduleLocalNotification</code> 方法安装相同的 <code>UILocalNotification</code> 对象,它<strong><em>将</em></strong>立即触发。 </p>

<p>在我看来这是一个基于 <a href="http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html" rel="noreferrer noopener nofollow">the documentation for scheduledLocalNotifications</a> 的错误,其中非常清楚地表明:</p>

<blockquote>
<p>...When you set property, UILocalNotification
replaces all existing notifications by calling
cancelLocalNotification: and then calling scheduleLocalNotification:
for each new notification.</p>
</blockquote>

<p>鉴于情况似乎并非如此,如果您的应用程序逻辑需要将过去安排的通知呈现给用户,则解决方法是调用 scheduleLocalNotification。</p>

<pre><code>UILocalNotification *ln = [init];
]; // two seconds ago
// ...

// the following line works as expected - the notification fires immediately
;// Using this line works as expected

// using the following does NOT work as expected - the notification does not fire
//application.scheduledLocalNotifications = ;
</code></pre>

<p>(我在 iOS 6 模拟器上测试过)</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UILocationNotfication.fireDate 过去 - 错误?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/12980584/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/12980584/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UILocationNotfication.fireDate 过去 - 错误?