stop playing sound push notification - ios

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.

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.

iOS push notification sound doesn't cancel on open

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.

Local Notifications not displaying in iOS 6

I am getting local notifications on time but they are just creating sound. I have disabled the "Do Not Disturb" option and set the Notification alert style as "Alert".
Is there any solution to this problem?
When the application is not active (in foreground), the OS will display an alert, play a sound and/or create a badge. When the application is active, there are no such thing happening. Instead, it'll call the application:didReceiveLocalNotification: method on your app delegate, in which you will have to create your own alert, play your own sound, etc.
Reference: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html

Invoke the application to play a music on receiving a push in iOS

I implemented Push notifications in iOS. I want my application to play a music (about 15sec) when I send a push message. I am currently getting the push message, and the music is being played only if the app is in the foreground.
If the app is in the background, it shows up a notification, and when i tap it, it then plays the music.
But how do I get the functionality of automatically playing the music? I think this can be done because, in iOS, Audio apps can run in the background. So I just want the music to be played when the app goes to background, and it receives a push.
Can anyone help me with this please?
Thanks,
Nithin
Yes, there are two ways to do this.
The traditional way before iOS 7 is to register for audio playing background mode and play a silent sound forever, this will keep your app running in the background for a long time until the user kills it, or it is killed due to low memory.
This way has many drawbacks, the user must launch your app once so that you can start playing the sound and every time the iPhone is shut off, the user need to relaunch your app when he turn on the iPhone.
Also this will consume a lot of battery. 20%-30% a day for an iPhone 5.
The new way after iOS 7.0 is to exploit background fetch and remote notification, you can play your sound while performing a background fetch, which has a 30 seconds running time. Also you can trigger a background fetch with a push notification.
This is an article about background fetch and push notification http://www.objc.io/issue-5/multitasking.html

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