iOS 中的闹钟应用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9445090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:02:43  来源:igfitidea点击:

Alarm clock app in iOS

iphoneiosuilocalnotification

提问by AmitK

I have to create an alarm clock app for the iPhone in which user can set alarm time and sound. In doing so, I have used UILocalNotification to set off the alarm.

我必须为 iPhone 创建一个闹钟应用程序,用户可以在其中设置闹钟时间和声音。为此,我使用了 UILocalNotification 来触发警报。

Now, in UILocalNotification, first we get Notification Alert with option "Close" and "View". If the user taps on "View", then my delegate receives the application:didReceiveLocalNotification:message and the alarm sound plays.

现在,在 UILocalNotification 中,首先我们获得带有“关闭”和“查看”选项的通知警报。如果用户点击“查看”,则我的代表会收到application:didReceiveLocalNotification:消息并播放警报声。

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?

但是在系统原生的闹钟应用中,我们看不到通知提醒;它只是直接播放闹钟声音。我怎样才能让我的应用使用这种行为?

回答by MyCSharpCorner

I agree with David. iOS built-in alarm app gets a special treatment from Apple to sound > 30 seconds while in the background. 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. This is not available to other apps built outside of Apple, so don't even try to compete with it. 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: i.e. "Sounding for a long period, in any state the app is in (active, background, or closed)"

我同意大卫。iOS 内置闹钟应用得到 Apple 的特殊处理,可在后台发出超过 30 秒的声音。它似乎正在使用一些私有 API 使其声音更长时间,即使它在后台或关闭时也是如此。这不适用于 Apple 之外构建的其他应用程序,因此甚至不要尝试与它竞争。如果你在 App Store 购买所有的闹钟应用(又名:苹果认可的)并一一试用,你会发现它们在闹钟最重要的功能上缺少内置的闹钟应用:即“发声”在很长一段时间内,应用程序处于任何状态(活动、后台或关闭)”

Your best bet to simulate a background alarm is to make use of UILocalNotification for 30 seconds. 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.

模拟后台警报的最佳选择是使用 UILocalNotification 30 秒。如果您真的想唤醒沉睡者,请安排间隔 60 秒或类似时间启动其中的五个通知,希望它们能在第 5 个通知结束之前醒来。

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 制造的内置时钟应用程序。

回答by David V

This alarm behavior is not available to iOS apps. 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.

此警报行为不适用于 iOS 应用程序。您可以创建本地通知并为其添加声音,但声音长度是有限的。您不能像内置闹钟那样持续播放声音。

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.

一种选择是重复警报。所以你的应用程序会发送一个警报,等待一段时间然后再发送一个。您重复指定的次数或直到用户与您的应用进行交互。我不认为这会提供很好的用户体验,所以我不推荐它。

回答by Shubhank

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. You can get more info here.

注意本地通知声音必须只有 30 秒或更短时间才能播放。您可以在此处获取更多信息

回答by Justin Garcia

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. 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.

如果您启用“音频、AirPlay 和画中画”和“远程通知”后台模式,您可以使用静音推送通知唤醒设备然后开始播放音频,只要您正在播放音频将不被限制为30秒,以响应于像通常的通知。如果用户手动杀死了应用程序,这将不起作用,但如果用户这样做,您可以发送本地通知,要求他们重新打开应用程序。