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

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.

Related

How to create an alarm timer in react native

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?

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.

Is it possible to change the display duration of a UILocalNotification?

I need to display a local notification and have it remain on screen longer than the default 4-5 seconds, preferably until the app itself removes it. I've seen other apps (e.g. Pandora) that manage to do this somehow (maybe a push notification?), but I can find no duration property on UILocalNotification or in UIApplication methods like presentLocalNotificationNow:, scheduleLocalNotification:, etc. Neither the documentation nor any of the tutorials I've found address the display time at all. Is this something that just can't be done with local notifications?
A couple of solutions here and I would not recommend either:
1- You can request from the user to go to settings > Notification Center > your app. And change the alert style from Banner (default) to Alerts. This will present the user an alert similar to the alert presented when the app is in the foreground. The user would have to dismiss the alert versus the banner style notification that just appears/disappears. Unless this is a corporate app and you have the users buy in, I would not go that route as this could annoy the user.
2- I tested the sound clip method and yes, if you present a notification with a clip < 30seconds; the notification will stay on the (top of the) screen until the sound clip is finished playing. Having said that, if the user taps any of the volume button (to reduce the sound for example), the notification is immediately dismissed even before its end! I think though that the purpose of the notification is a gentle reminder and, lasting more than the typical 4-5 seconds goes against the norm and, it might annoy the user (or the user might think something is stuck, phone froze, etc..). Here is the code anyway:
UILocalNotification *howLongCanANotificationLast = [[UILocalNotification alloc]init];
howLongCanANotificationLast.alertBody=#"I am a notification";
howLongCanANotificationLast.soundName=#"musicfilename.mp3";
[[UIApplication sharedApplication] presentLocalNotificationNow:howLongCanANotificationLast];
Hope this helps.

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.

Cancel a push notification sound while it is playing

Tried to find this already but can't find any questions which are asking quite the same thing.
Basically I have set up my push notifications, everything works fine and they are received both when the app is open and closed. However, the push noise we are using is quite long and I'm not sure if it's possible to cancel the sound once they press the "ok" button on the alert.
I have tried to utilise the mute toggle switch, but this only seems to come into play at the moment that sound starts to play, if it's off the sound plays, if it's on then it doesn't. If I toggle the switch mid-sound it has no effect. However, if I just play a regular sound clip in my app (not a push notification sound) and use the toggle switch then the sound stops/starts as you would expect.
Is there a way to cancel the sound? Or is it treated differently as a system sound of some kind?
Edit: I've been trying to work this out myself for the last few days, and I'm coming to the conclusion that there is no way to cancel the push alert sound mid-sound. Can anyone confirm that this is definitely the case?
Edit2: For some reason the xcode tag has been removed - with the reason being that it is nothing to do with xcode. I feel maybe my issue was not clear - I AM using xcode to build the app, and I am looking for a way to programatically control whether the push notification sound is heard or not. Thanks.
No answers given, having tried and failed over the last few months I can only assume this is not possible (just in case anyone has the same issue in the future).
It works for me:
[[UIApplication sharedApplication] cancelAllLocalNotifications]
As of iOS 10, you can remove all notifications from Notification Center by calling
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
The audio stops playing and all the notifications are removed from Notification Center.
Alternatively, to stop the sound for only a specific notification, you can use:
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [String])
This stops the audio for only the given notifications, and removes them from Notification Center.
To get the ids for all the delivered notifications, use
UNUserNotificationCenter.current().getDeliveredNotifications { notifications in
let ids = notifications.map { $0.request.identifier }
}

Resources