2

I want to create an "alarm clock" -app which executes an alarm tone at a configurable time, also when the app is closed.

For android I found AlarmManager but it doesn't work if the app is closed. Is this possible to achieve this functionality for iOS and Android?

Harald
  • 326
  • 4
  • 16
  • What's the difference between your app and system alarm clock? Can you use a local notification to achieve that? – nevermore Jun 06 '19 at 02:43
  • It is a app in the medical area; the patient has to do certain exercises at specific times. So you could do this with the system-alarm-clock and just open the app manually. – Harald Jun 06 '19 at 07:19

2 Answers2

2

You can fire a local notification at specific times to alert people do certain exercises. Every time you need to add an exercise, just add a local notification with specific time, and you can set the sound, content, isRepeat and etc.

For how to set a notification:

If you are using Xamarin.forms: you have to use dependency-service to implement the local-notifications in iOS and android project.

For example, in your xamarin.forms project:

public interface ISetLocalNotification
{
    void noti(string time, string content...);
}

And implement in both iOS and Android project.

iOS: local-notifications-in-ios

Android: local-notifications-in-android

And when you want to use :

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        DependencyService.Get<ISetLocalNotification>().noti("12:00","123"...);

    }
} 
nevermore
  • 15,432
  • 1
  • 12
  • 30
  • As I understand this only will send a notification with a sound. Is there a possibility to start an annoying sound which the user has to deactivate like an alarm clock? – Harald Jun 06 '19 at 15:56
  • @Harald Check the two answers in [this thread](https://stackoverflow.com/questions/6543726/how-do-i-play-an-alarm-sound-for-more-than-30-seconds-like-the-alarm-clock-pro-a). And here is the [document](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/SupportingNotificationsinYourApp.html#//apple_ref/doc/uid/TP40008194-CH4-SW1) that Apple says you can not play sound more than 30 seconds. – nevermore Jun 10 '19 at 01:45
-1

NOT THE SOLUTION:


There's several ways of doing this. It wasn't clear if you are using Xamarin Forms or not. But assuming you are using Xamarin forms, here's a great example with an official Xamarin video. But essentially, you are going to be running some code in the background while your app is not open. Just enable the permissions to do so, and check for time, so that the app plays the alarm when it is time.

If you are not using forms, you don't need to create the interface, and so it's much easier.

Saamer
  • 4,687
  • 1
  • 13
  • 55
  • Sorry for -1, but the link that you have provided for iOS is not for running the background code at desired time. The code will run in the background, but when the system decides to do so. Also while I don't see anything wrong on that link, there is nothing official there. – Ivan Ičin Jun 05 '19 at 20:44
  • @IvanIčin good catch! I fixed it. I was looking at the other really good example and ended up sharing the wrong one ( https://xamarinhelp.com/xamarin-background-tasks/ ) – Saamer Jun 05 '19 at 20:51
  • I am not sure about this. This is about keeping the app running while in the background... It should be fairly limited according to the description. – Ivan Ičin Jun 05 '19 at 22:11
  • How would this look like with a service? There is the portability to trigger a service every 15 min (smallest interval) but this is not precise enough for a alarm clock. – Harald Jun 06 '19 at 15:51