TheBestBigAl
TheBestBigAl

Reputation: 1230

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.

Upvotes: 4

Views: 1951

Answers (3)

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 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 }
}

Upvotes: 0

Andrew Kovzel
Andrew Kovzel

Reputation: 601

It works for me: [[UIApplication sharedApplication] cancelAllLocalNotifications]

Upvotes: 0

TheBestBigAl
TheBestBigAl

Reputation: 1230

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).

Upvotes: 1

Related Questions