The first load of an app with local notification custom sounds works correctly. When the app is updated (by Xcode, TestFlight or App Store) the app plays only the default sound. Behavior is the same with either new UNUserNotifications or deprecated UILocalNotifications. Behavior only occurs on iOS 10. The custom sound is a 2 second caf file in the application bundle (and it plays correctly multiple times until the app is updated). Notification permissions including Sounds are properly granted. Deleting the app and reloading causes the custom sound to play correctly again.
I've filed a RADAR but would appreciate any suggestions.
Test app:
@interface AppDelegate ()
@end
@implementation AppDelegate
BOOL soundsAllowed;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"application didFinishLaunchingWithOptions");
[self requestAuthorizationForNotifications];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(@"applicationWillResignActive");
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"applicationDidEnterBackground");
[self scheduleLocalNotification];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(@"applicationWillEnterForeground");
// check if we are still authorized
[self requestAuthorizationForNotifications];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"applicationDidBecomeActive");
}
- (void)applicationWillTerminate:(UIApplication *)application {
NSLog(@"applicationWillTerminate");
}
- (void)requestAuthorizationForNotifications {
NSLog(@"requestAuthorizationForNotifications");
soundsAllowed = NO;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (error) {
NSLog(@"request authorization error: %@", error);
} else {
NSLog(@"request authorization no error");
[self checkSoundsAllowed];
}
}];}
// examine notification settings to determine if sounds are allowed, save result
- (void)checkSoundsAllowed {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
soundsAllowed = (settings.soundSetting == UNNotificationSettingEnabled);
NSLog(@"soundsAllowed: %@", (soundsAllowed ? @"true" : @"false"));
if (soundsAllowed) {
// clear any existing notifications
[center removeAllPendingNotificationRequests];
}
}];
}
- (void)scheduleLocalNotification {
if (!soundsAllowed) {
NSLog(@"scheduleLocalNotification soundsAllowed: false. return without scheduling");
return;
}
// Set up the request content
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"Title";
content.body = @"Body";
content.sound = [UNNotificationSound soundNamed:@"Test.caf"];
// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5.f repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
content:content trigger:trigger];
// Schedule the notification.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"addNotificationRequest succeeded! content.sound: %@ default sound: %@", content.sound, [UNNotificationSound defaultSound]);
}
}];
}
@end