菜鸟教程小白 发表于 2022-12-13 08:23:26

iOS UILocalNotification 仅在应用未运行时发出声音(无警报)


                                            <p><p>我正在使用远程通知来唤醒我的应用并有条件地触发 UILocalNotification。</p>
<p>我发现如果在应用程序未运行时触发此通知,我得到的只是通知声音(没有警报/通知)。如果应用程序在后台,它会按预期工作。</p>
<p>在 AppDelegate 中注册远程通知:</p>
<pre><code>let settings = UIUserNotificationSettings(forTypes: (.Badge | .Sound | .Alert), categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
</code></pre>
<p>创建/触发 UILocalNotification(作为 <code>didReceiveRemoteNotification</code> 的结果)</p>
<pre><code>var notification = UILocalNotification()
notification.alertTitle = &#34;My App&#34;
notification.alertBody = &#34;Hello World!&#34;
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
</code></pre>
<p>据我从文档中可以看出,当应用程序在后台并且根本不运行时的通知行为应该是相同的..</p>
<blockquote>
<p>When the system delivers a local notification, several things can happen, depending on the app state and the type of notification. If the app is not frontmost and visible, the system displays the alert message, badges the app, and plays a sound—whatever is specified in the notification. If the notification is an alert and the user taps the action button (or, if the device is locked, drags open the action slider), the app is woken up or launched.</p>
</blockquote>
<p>来源:<a href="https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/index.html" rel="noreferrer noopener nofollow" title="UILocalNotification Documentation">UILocalNotification Documentation</a> </p>
<p>非常感谢您深入了解这里发生的事情!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>问题与本地通知的显示无关。</p>

<p>正如您在问题中所说,您使用远程通知来触发打开您的应用程序,然后运行代码以有条件地显示本地通知。</p>

<p>这不起作用的原因是:</p>

<p>您显示本地通知的代码<strong>不</strong>在用户强制关闭应用程序(通过向上滑动)后从远程通知运行!</p>

<p>您可以在此处的开发者论坛中找到更多相关信息:
<a href="https://devforums.apple.com/message/873265#873265" rel="noreferrer noopener nofollow">https://devforums.apple.com/message/873265#873265</a> (需要登录开发帐户)</p>

<blockquote>
<p>Also keep in mind that if you kill your app from the app switcher (i.e. swiping up to kill the app) then the OS will <strong>never relaunch the app regardless of push notification or background fetch</strong>.In this case the user has to manually relaunch the app once and then from that point forward the background activities will be invoked. </p>
</blockquote></p>
                                   
                                                <p style="font-size: 20px;">关于iOS UILocalNotification 仅在应用未运行时发出声音(无警报),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/30948864/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/30948864/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS UILocalNotification 仅在应用未运行时发出声音(无警报)