Stopping ios 7 remote notification sound
Asked Answered
G

3

3

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.

Gonidium answered 1/10, 2013 at 20:3 Comment(5)
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.Kenna
you might want to try [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1]; [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];, thats actually what I am doingGonidium
That did the trick yeah. What I bountied is a real solution, though.. This feels more like a bad workaround.Kenna
oh I'm with you I'd love to know a real solution as well.Gonidium
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 #145373. You may want to file a duplicate bug report, as I have.Gonidium
G
7

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];
Gonidium answered 9/10, 2013 at 13:21 Comment(5)
Where do you have to put this code to make it happen? I pasted this code in : - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification But the notification sound keeps on going?!?Deoxygenate
Are you trying to stop the sound for the remote notification? if so didReceiveLocalNotification is, like it says, only for local notifications. Read up on how to handle remote notifications as theres a bit to it, but in short you can put it in didReceiveRemoteNotification and didFinishLaunchingWithOptionsGonidium
Yes I am putting it in the DidReceiveLocalNotification and yet the sound is not stopping. Any suggestions please?Deoxygenate
I remember reading that in some cases didFinishLaunchingWithOptions is called instead, try putting it there as well?Gonidium
This workaround seems to be working when the local notification has been triggered by PUSH only, does not work unfortunately when the notification has been triggered by the app itself :(Wilcher
D
0

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];
    }
}
Dardan answered 20/7, 2015 at 8:35 Comment(0)
G
0

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 }
}
Grajeda answered 1/6, 2020 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.