Azat
Azat

Reputation: 6795

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?

Upvotes: 4

Views: 458

Answers (2)

SlimeBaron
SlimeBaron

Reputation: 775

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.

Upvotes: 0

vissh
vissh

Reputation: 19

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.

Upvotes: 1

Related Questions