3
votes

In iOS 7, when a user swipes one of my notifications from the lockscreen and is taken to my app, the notification sound keeps playing (unlike iOS 6). Is there any way to programmatically stop that sound when my app launches in iOS 7?

NOTE: see the accepted answer for a shoddy workaround.

3
On my side, setApplicationIconBadgeNumber: 0 didn't work. Note that this seems to be a bug: devforums.apple.com/message/888091 But Apple didn't communicate on this.Gui13
you might want to try [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1]; [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];, thats actually what I am doingMohamed Hafez
That did the trick yeah. What I bountied is a real solution, though.. This feels more like a bad workaround.Gui13
oh I'm with you I'd love to know a real solution as well.Mohamed Hafez
I'm pretty sure this is a bug on apple's end after reading the dev forum link you posted, Apple doesn't let you see other people's bug reports - they use the fact that multiple people are reporting the same bug to assign priority to bugs, according to stackoverflow.com/questions/144873/…. You may want to file a duplicate bug report, as I have.Mohamed Hafez

3 Answers

7
votes

I'm pretty sure this is a bug on Apple's end, see devforums.apple.com/message/888091 (thanks Gui13). File a duplicate bug report to get Apple to pay attention to it, as that is how Apple assigns priority to bugs. In the meantime, the following will work but will also clear all of your notifications in the notification center, which of course is a shoddy workaround, but in my case is worth it until this gets fixed:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];    
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
0
votes

it doesn't help by using

[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];    
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];

after entering the app by clicking the notification.

I have solved this problem by sending another empty notification when dealing with the notification with sound:

if (notification.soundName != nil) {
    if (IS_IOS7) {
        UILocalNotification *emptyNotification = [[UILocalNotification alloc] init];
        emptyNotification.timeZone = [NSTimeZone defaultTimeZone];
        emptyNotification.fireDate = [NSDate date];
        emptyNotification.alertBody = @"";
        [[UIApplication sharedApplication] scheduleLocalNotification:emptyNotification];
    }
}
0
votes

For iOS 7, the accepted answer may be the only viable option. For developers who have come here that can support a minimum of iOS 10, this works.

You can remove all notifications from Notification Center by calling

UNUserNotificationCenter.current().removeAllDeliveredNotifications()

This has a very similar affect as the accepted answer: the audio stops playing and all the notifications are removed from Notification Center.

An improved solution is to 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 }
}