简体   繁体   English

iOS中的闹钟应用程序

[英]Alarm clock app in iOS

I have to create an alarm clock app for the iPhone in which user can set alarm time and sound. 我必须为iPhone创建一个闹钟应用程序,用户可以在其中设置闹钟时间和声音。 In doing so, I have used UILocalNotification to set off the alarm. 在这样做时,我使用了UILocalNotification来引发警报。

Now, in UILocalNotification, first we get Notification Alert with option "Close" and "View". 现在,在UILocalNotification中,首先我们通过选项“关闭”和“查看”获得通知警报。 If the user taps on "View", then my delegate receives the application:didReceiveLocalNotification: message and the alarm sound plays. 如果用户点击“查看”,则我的代理人会收到application:didReceiveLocalNotification: message并播放警报声音。

But in the system-native alarm app, we don't see a notification alert; 但在系统本机警报应用程序中,我们看不到通知警报; it just directly plays the alarm sound. 它只是直接播放警报声。 How can I have my app use this behavior? 如何让我的应用程序使用此行为?

I agree with David. 我同意大卫的观点。 iOS built-in alarm app gets a special treatment from Apple to sound > 30 seconds while in the background. iOS内置闹钟应用程序获得Apple的特殊处理,在后台播放时声音> 30秒。 It seems to be using some private API to make it sound for a longer time even when it is in the background or closed. 它似乎使用一些私有API使它在较长时间内发出声音,即使它在后台或关闭时也是如此。 This is not available to other apps built outside of Apple, so don't even try to compete with it. 这不适用于在Apple之外构建的其他应用程序,所以甚至不要尝试与它竞争。 If you buy all Alarm clock apps in the App Store (aka: approved by Apple) and try them one by one, you will see that they lack behind the built-in alarm app in the most important function of an alarm: ie "Sounding for a long period, in any state the app is in (active, background, or closed)" 如果您购买App Store中的所有闹钟应用程序(又名:Apple批准)并逐一尝试,您会发现它们在警报的最重要功能中缺少内置警报应用程序:即“Sounding”在很长一段时间内,应用程序处于任何状态(活动,背景或关闭)“

Your best bet to simulate a background alarm is to make use of UILocalNotification for 30 seconds. 模拟背景警报的最佳选择是使用UILocalNotification 30秒。 If you really want to wake up your heavy sleeper, schedule to fire up five of these notifications sixty seconds apart or something like that, in the hopes they will wake up before the 5th notification ends. 如果你真的想要唤醒沉重的睡眠者,安排将这些通知中的五个发出六十秒或类似的东西,希望他们会在第五次通知结束前醒来。

Notice that with this method, your user who misses the alarm will get 5 popups on their screen when they wake up (less if they wake up during one of the alarm popups), so they'll be spending their first seconds of their morning closing those popups - not the greatest user experience. 请注意,使用此方法,未命中警报的用户在唤醒时会在屏幕上显示5个弹出窗口(如果他们在其中一个警报弹出窗口中醒来,则会减少),因此他们将花费他们早上关闭的第一秒钟那些弹出窗口 - 不是最好的用户体验。

Conclusion: Alarm Clock apps are good for foreground use only. 结论:闹钟应用程序仅适用于前台使用。 Best Alarm app out there that will work in any state is the built-in Clock app made by Apple. 适用于任何州的最佳闹钟应用程序是由Apple制造的内置时钟应用程序。

This alarm behavior is not available to iOS apps. iOS应用程序无法使用此警报行为。 You have the ability to create local notifications and add sounds to them, but the sound length is limited. 您可以创建本地通知并为其添加声音,但声音长度有限。 You cannot have a sound continuously play such as on the built-in alarm. 您无法连续播放声音,例如内置闹钟。

One option is to repeat the alerts. 一种选择是重复警报。 So your app would send an alert, wait some time then send another. 因此,您的应用程序将发送警报,等待一段时间再发送另一个。 You repeat a specified number of times or until the user interacts with your app. 您重复指定的次数或直到用户与您的应用互动。 I don't think this would provide a great user experience, so I don't recommend it. 我不认为这会提供出色的用户体验,所以我不推荐它。

I am pretty sure you can have your own custom local notification sound. 我很确定你可以拥有自己的自定义本地通知声音。

Example code localNotif.soundName = @"alarmsound.caf"; 示例代码localNotif.soundName = @"alarmsound.caf";

Note local notification sound will have to be of only 30 seconds or less to be played. 注意本地通知声音必须仅为30秒或更短时间才能播放。 You can get more info here . 你可以在这里获得更多信息

If you enable the "Audio, AirPlay, and Picture in Picture" and "Remote notifications" Background Modes, you can use a silent push notification to wake up the device and then start playing audio, and as long as you're playing audio you won't be limited to 30 seconds to respond to the notification like usual. 如果您启用“音频,AirPlay和画中画”和“远程通知”后台模式,您可以使用静音推送通知来唤醒设备,然后开始播放音频,只要您正在播放音频,不会像往常一样响应通知30秒。 This won't work if the app has been killed manually by the user, but if the user does that you could send a local notification asking them to re-open the app. 如果应用程序已被用户手动杀死,则此操作无效,但如果用户这样做,您可以发送本地通知,要求他们重新打开应用程序。

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

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