iOS push notification sound doesn't cancel on open - ios

I am creating an application that uses Apple Push Notifications with custom sound (about 15 sec length). If I open notification when phone is in use, sound terminates, but if I open notification when it wakes up phone from sleep mode, sound continues to play until the end. How can I avoid unnecessary push sound playing?

I know that it is quite late, but it turns out that apple does not provide any API to manage push notification sounds, so you can’t stop it from playing. I would like to suggest you use shorter push notification sound. This also makes sense because intent of push notification is to shortly inform user about new event, not disturbing him for a long time.

As of iOS 10, you can remove all notifications from Notification Center by calling
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
The audio stops playing, but 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 }
}
Of course, the downside is that the notifications are removed from the user's Notification Center.

Related

Don't Play iOS Push Notification Sound While the app is in active/foreground

I am getting Push Notification while the application is in foreground. When the application is in foreground state, I don't want it to play any sound. How I can do it?
Don't pass .sound into the callback for userNotificationCenter(_:willPresent:withCompletionHandler:)
https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate/1649518-usernotificationcenter

Will silent notitifcation be displayed like normal notifications even if didReceiveRemoteNotifications is not called by iOS

Right now, my iOS app receives normal (non-silent) push notifications.
iOS device displays notifications as expected.
Now, I want to convert normal notifications to silent by adding "content-available=1" along with all other user visible keys.
To be able to perform bg tasks on notification trigger
My understanding is iOS may not always call didReceiveRemoteNotifications for silent notifications.
My question is would silent notifications will get displayed by iOS (if it contains user visible keys) even if iOS decides not to call didReceiveRemoteNotifications
My expectation is silent notifications should work as is like normal notifications with an advantage of didReceiveRemoteNotifications getting called as and when possible (Best Effort)
I don't want to lose normal notification behavior by switching to silent notifications.
As per this link
For a silent notification, take care to ensure there is no alert,
sound, or badge payload in the aps dictionary. If you don’t follow
this guidance, the incorrectly-configured notification might be
throttled and not delivered to the app in the background, and instead
of being silent is displayed to the user
So if the keys alert, sound and badge are added then that notification will be displayed like a normal push notification.

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.

stop playing sound push notification

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.
I recieve push, sound begins play. For some reason i need to cancel sound without active app.
Skype have this feature. When app is suspended, incoming call start to play sound by push. After call cancelation sound stops.
UPD:
Is there a way to cancel the sound like Skype?
When Skype app is suspended iPhone receives PUSH for incoming call. Sound begins playing. When call cancel at other side iPhone stops play sound by second push. Anyone knows how this feature works?
You specify a bitmask of notification types when you register for notifications. If you don't want a sound to be played when notifications come in, remove UIRemoteNotificationTypeSound from that bitmask
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge)];
I think they're using invisible push to trigger a local notification. Local notification may be cancelled, so they then cancel it when needed.

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