Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILocalNotification doesn't stop the sound when the application is brought to foreground by sliding on the alert

Tags:

ios

ios7

When my application receives a network messages in the background, it create a UILocalNotification with sound and invokes the presentLocalNotificationNow.

    UILocalNotification* localNotif = [[UILocalNotification alloc] init];
    localNotif.alertBody = [NSString stringWithFormat:@"%@: \r%@ \r%@", NSLocalizedString(@"XXXXXXXXXXX", nil), XXXXX, XXXXXXXXXXX];
    localNotif.alertAction = NSLocalizedString(@"View", nil);

    localNotif.soundName = [NSString stringWithFormat:@"%@.%@", XXXXTONE_LONGVERSION_FILENAME, XXXXTONE_FILENAME_EXT];
    [[UIApplication sharedInstance] presentLocalNotificationNow:localNotif];
    [localNotif release];

This alert is shown with sound as expected. But I have trouble stopping the sound in some cases.

1) When there is screen lock and this local notification alert is shown:

  • if the user unlocks the screen by sliding on the alert, the sound does NOT stop.
  • if the user unlocks the screen by sliding "> slide to unlock", it does stop the sound.

2) When the application is background with out screen lock, the alert sound stops normally upon application coming foreground.

I do cancel the scheduled local notifications when app comes foreground in didReceiveLocalNotification and applicationDidBecomeActive.

    application.applicationIconBadgeNumber = 0;
    [application cancelAllLocalNotifications];
like image 428
Phani2013 Avatar asked Jan 14 '14 19:01

Phani2013


2 Answers

It's a known bug of iOS7 which is annoying as hell.
You'll have the exact same problem if you use remote notifications.

The workaround for this is to change the application badge count twice, with at least a step to the value 0. I have this method in my app for that:

- (void)fixRing{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        // iOS7 fix for notification sound not stopping.
        // see http://stackoverflow.com/questions/19124882/stopping-ios-7-remote-notification-sound
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
    }
}

Don't forget to update the badge count to the real value sometimes after.

EDIT: Actually, this doesn't work for local notifications, but only for remote (push) notifications. [application cancelAllLocalNotifications]; doesn't work, neither does [application cancelLocalNotification:notif];...
This is a bug that I've reported to Apple, and is in "process" at the time of writing.
I've tried plenty of workarounds (push a notif with a small sound to erase the long sound, use a timer and reiterate the notification with a shorter sound instead of letting it linger for 30s, etc..) but none worked.

Note that Google Hangout seems to suffer from this problem as well.. so I guess there's no real solution.

like image 68
Gui13 Avatar answered Sep 21 '22 17:09

Gui13


In your AppDelegate in didFinishLaunchingWithOptions you get your Notification like that:

UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
    // do something with it
}

Check, if you can stop the alert from there with cancelLocalNotification:

like image 28
Christian Avatar answered Sep 24 '22 17:09

Christian