iosunnotificationrequest

UNNotification: Custom Sound for LocalNotification is not playing in iOS10


I'm firing a local notification. Since UILocalNotification class is deprecated in iOS 10, I have used UserNotifications.framework.

When I try to set the custom sound for the notification, the default sound is playing all time.

Here is my code:

- (IBAction)fireLocalNotification:(id)sender {
    
    UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
    content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" arguments:nil];
    content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body"
                                                         arguments:nil];
    content.sound = [UNNotificationSound soundNamed:@"sound.mp3"];
    
    // Deliver the notification in five seconds.
    UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
                                                  triggerWithTimeInterval:5 repeats:NO];
    
    UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
                                                                          content:content trigger:trigger];
    
    // Schedule the notification.
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    [center addNotificationRequest:request withCompletionHandler:^(NSError *error){
        
        if(!error){
            
            NSLog(@"Completion handler for notification");
        }
        
    }];
}

My sound is fine; sound.mp3 is present in project bundle itself.

More info: https://developer.apple.com/reference/usernotifications/unusernotificationcenter?language=objc


Solution

  • Try deleting the app from the device, clean and run the app again on device.

    Sometimes, resources are not properly updated; I think that is the problem in your case.