2

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

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Suhas Arvind Patil
  • 1,732
  • 1
  • 19
  • 31

2 Answers2

3

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Wolverine
  • 4,264
  • 1
  • 27
  • 49
1

Long audio-files are not supported.

Is your file longer then 30 seconds? If yes, it will not work. Only files shorter then 30 seconds are available. If the file is longer then 30 seconds then it will be replaced with default sound.

UNNotificationSound documentation

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Vahagn Gevorgyan
  • 2,635
  • 19
  • 25