Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 744
Suhas Arvind Patil Avatar asked Nov 17 '25 11:11

Suhas Arvind Patil


2 Answers

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.

like image 158
Wolverine Avatar answered Nov 19 '25 03:11

Wolverine


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

like image 29
Vahagn Gevorgyan Avatar answered Nov 19 '25 01:11

Vahagn Gevorgyan