How to create an alarm timer in react native - ios

I'm trying to replicate the native iOS timer with a light theme.
I've tried achieving the repeating sound functionality in the following ways and none worked while the app is in the background:
(I'm using react-native-background-timer and notifee)
Recursively fire a "play sound" function when the timer is up - doesn't work on background, the sound is played only when I open the app
Recursively schedule local notifications for when timer is up - only the first one fires, i.e. the notification when the timer is up, but after that nothing
If going with the notification route, I'd like to do something like:
notifee.displayNotification({ loopSound: true, ongoing: true })
Currently my last resort is scheduling a bunch of notifications ahead of time while the app is in the foreground, about 6 seconds apart each.
This is hacky and also if the user looks at the lock screen or notification center they'd see all these notifications which are one and the same.
I tried doing the same with passing the notification id, i.e. to update the same notification instead of creating a new one each time, but then the notification is only displayed once, at the latest scheduling.
Any ideas how to go about it?

Related

How to make an iOS VoIP app obey Do Not Disturb when ringing?

One would think it would be essential for a VoIP app to obey the same rules as the stock phone app but it turns out to be almost impossible to implement ringing correctly. Several things I tried:
Local push notifications with ring sound.
Good: obeys both Silent and DND modes.
Bad: the sound can be no longer than 30 seconds, and it only vibrates once when the notification appears. So to achieve the ringing effect the notification has to be re-pushed e.g. every 6 seconds, effectively spamming the notification center. Also push notifications do not sound/vibrate if the app is active so the app has to detect that and ring differently.
AudioServicesPlayAlertSound().
Good: proper API seemingly designed specifically for this task. Obeys silent mode.
Bad: completely ignores Do Not Disturb mode, the sound and vibration come right through.
Use AVFoundation to play the ring sound.
Good the sound plays.
Bad: does not support vibration, does not support silent/DND modes. Essentially not usable as a ringer.
Is there a better way? Or did Apple completely miss this use case?
As you say in your 3 options, only a UILocalNotification actually obeys silent/DND mode.
The problems with it can be solved.
Spamming the notification center: I think that works quite well. You can cancel your previous notification immediately before you fire off a new one, so there will always be only 1 outstanding notification.
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Vibration problem: You should be able to call this: AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); in the same place where you call your local notification over and over again with a timer until the call ends or the users acknowledges the call. With the VOIP background setting on it should work in the background.
As you stated in option 2 the vibrate will not follow DND mode, but it's just vibration. If you spam the notification center that will vibrate once every time the notification comes in so you may not need to explicitly start vibrating if that's enough for you.
Good luck.

run ios app in background

I'm new to iOS programming, I've managed to create a countdown timer that counts down to 0 and when the timer reaches 0 an UIAlertView pops up to say that the timer is done.
Although my problem is when I minimize the app or if the screen goes to sleep my countdown timers stops and my alert won't show up until I start the app again and let it count down to 0.
So my question is basically, how do I let the app run in the background, or at least let it count down in the background?
Thanks in advance!
The easiest way will be to create a "local notification". When your app enters the background, cancel your timer, but schedule a local notification that will fire at the fireDate corresponding to when your timer hits "0". If the local notification fires while your app is in the background, the user will be presented with an appropriate alert and will have a chance to launch your app.
See the Apple Local and Push Notification Programming Guide for instructions on how to schedule and then handle a local notification.

How can I keep playing the UILocalNotification sound until user dismiss it?

As titled, I use UILocalNotification for alarm purposes. But the sound is only played once.
I would like to keep the sound playing until user dismisses the notification, and only one notification item will be showed in Notification Center.
There are two ways doing this, but none is well enough.
1. First: I can fire up to 64 notifications one by one, when the alarm time arrives, the alarm sound will be played multiple times(up to 64 times), as if it's been played endlessly. But this will leave too many items in Notification Center, which annoying me. Is there any way to combine these items showed in Notification Center into one?
2. Second: I assume there is a way to fire a single notification, and keep playing its sound. But right now I can only play sound within 30 seconds once.
1) For first point it is not possible.
2) You can not playing sound constantly when single notification arrives until user dismisses notification manually.
so,both the scenarios are not possible. Please read briefly here.

UILocalNotification - Need to vibrate for a longer duration during the alert / notification

Overview
In my iOS project, I am using UILocalNotification,
when a notification is fired, a custom sound is played.
the custom sound plays for about 20 seconds,
the phone vibrates only once at the start
What I want to do:
Presently the phone vibrates only once at the start. I want it to vibrate repeatedly for 20 seconds just like in Apple's alarm / Timer app before the user pressed on the action button ?
Question
During the alert is it possible to make the phone to vibrate for the 20 seconds before the user can attends to the notification or clicks on the action button ?
Problem
Since my app doesn't have any control till the user presses on the action button I am not sure how I can make it vibrate
The app might be closed when the notification is pops
Is there a way to do this ?
Schedule a timer that invokes the vibration function several times with some delay between calls. Don't forget to add an option to disable the vibration, cause you might get your app rejected from the app store because of this.

UILocalNotification how to call certain method if application is in background

I am creating player application, which is playing audio streams through internet. I want to add alarm functionality in my app - in particular time my player begins to play audio stream, I am trying to use UILocalNotification mechanism. But I've difficulties with it when my application in background mode, I can't call 'play' method, when notification is receiced (can't without user interaction). May be it is impossible?
But I bought this application:
http://itunes.apple.com/us/app/radio-alarm-clock-mp3-radio/id380271167?mt=8
And it seems like radio can start playing when local notification is received. Alarm can start playing radio when my app is in background mode.
Earlier I was trying to use NSTimer for this, but when my app goes to background, timer stops. If I use beginBackgroundTaskWithExpirationHandler: it works only 10 minutes. My app has special flag in plist, what is is audio application, and can playing music in background. In this case timers are working. But if I stop playing and go to background, timer is not working.
When I use \Radio Alarm Clock' application, I hear 'white noise' from dinamic, when music in not playing. May be it is the secret of this application?
Can you help me with my problem? Thanks.
maybe it's too late.
I had a look to the app you've mentioned at http://itunes.apple.com/us/app/radio-alarm-clock-mp3-radio/id380271167?mt=8 and yes, I think you are absolutely right, the only way to achieve that the application remains active while in background is to play a fake sound while it is in the background, which should be prohibited by Apple.
I've also seen that they don't use the remote iPod control, and this was strange at a first look.
At the end my opinion is that they do the following:
Avoid the call to beginReceivingRemoteControlEvents which allows to activate the iPod controls while in background (in fact they don't have it)
In this way, the status bar doesn't show the play icon while
the app plays audio
When the app goes in background, it probably plays a no sound periodically (once every 10 secs for example), in this way the app remains active
I saw that they also avoided to manage interruptions, for example in case another app is in foreground and plays music. Again Apple should have rejected the app for that reason, cos it is against the rules to follow while in background, but maybe they didn't see it during the acceptance tests.
So my interpretation is that they have intentionally missed to activate the iPod controls, just to avoid to show the play icon in the status bar while in background. In this way, the users are unaware that the app is active and is doing something strange after they close it.
In addition you can see that the app doesn't interrupt when another app plays in foreground a sound or audio, because otherwise they risk that the app doesn't restart on time when the alarm shpould fire.
That's just my idea of how they do that, and I think this is the only way for an audio app on iOS to remain active while it is in background and is supposed to be halted (well, in case Apple doesn't see the trick).
Have you tried adding this to appdelegate.m
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
// Call your method in here.
}
if you have can you add code for us to see what your doing.

Resources