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:
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];
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With