菜鸟教程小白 发表于 2022-12-11 19:48:14

ios - UNNotificationRequest 仅在 iPhone 解锁时播放自定义声音


                                            <p><p>我的应用正在下载一个 .wav 文件并将其移动到 Library/Sounds,然后通过 <code>UNNotificationRequest</code> 使用该声音的文件名安排本地通知。此请求按预期发送通知,但我添加的自定义声音<em>仅</em>在手机解锁时大声播放,而不是在通知发送到手机的锁定屏幕时播放。但是,如果我使用 <code>UNNotificationSound.default()</code> 而不是我的自定义声音,则默认推送声音将在锁定屏幕和设备解锁时播放。任何人有任何想法可能导致这个问题?我的代码很简单:</p>

<pre><code>let soundName = &#34;demoSound.wav&#34; // (or nil to use default)

let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests()

let content = UNMutableNotificationContent()
content.title = &#34;Title&#34;
content.body = &#34;Body&#34;;
content.sound = soundName.flatMap { UNNotificationSound(named: $0) } ??UNNotificationSound.default()

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let identifier = &#34;PushNotifierLocalNotification&#34;
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.add(request, withCompletionHandler: nil)
</code></pre>

<hr/>

<p><strong>编辑:对于捆绑的声音(拖放到 Xcode 中)这不是问题,但只有当我使用由我的应用下载并移至库/声音中的声音时,我才会看到上述问题。</strong ></p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>来自 <a href="https://en.wikipedia.org/wiki/WAV" rel="noreferrer noopener nofollow">here</a> ,你会注意到:</p>

<blockquote>
<p>Waveform Audio File Format (WAVE, or more commonly known as WAV due to its filename extension - both pronounced &#34;wave&#34;)<a href="https://en.wikipedia.org/wiki/Core_Audio_Format" rel="noreferrer noopener nofollow">3</a> (rarely, Audio for Windows) <strong>is a Microsoft and IBM audio file format</strong> standard for storing an audio bitstream <strong>on PCs</strong>.</p>
</blockquote>

<p>我假设 Apple 更喜欢原生 Apple 声音格式,例如 <a href="https://en.wikipedia.org/wiki/Audio_Interchange_File_Format" rel="noreferrer noopener nofollow">AIFF</a>或 <a href="https://en.wikipedia.org/wiki/Core_Audio_Format" rel="noreferrer noopener nofollow">CAF</a> ,两者都是由 Apple 开发的。</p>

<p>如果问题仍然存在,您能否使用 AIFF 或 CAF 声音文件格式重试并确认使用?</p>

<p>你也可以看看<a href="https://stackoverflow.com/questions/37536591/change-push-notification-sound" rel="noreferrer noopener nofollow">here</a> .</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UNNotificationRequest 仅在 iPhone 解锁时播放自定义声音,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/49020597/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/49020597/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UNNotificationRequest 仅在 iPhone 解锁时播放自定义声音