Karlo A. López
Karlo A. López

Reputation: 2668

Local Notification sound not playing?

I'm having this problem with the Local notifications, they are not playing the sound. I tested with the default one and the custom one that is obviously the one I want to play but nothing! I looked and a lot of people face this problem but none of their solutions worked for me.

UILocalNotification *local

- (IBAction)Dateaction:(id)sender {

    local = [[UILocalNotification alloc]init];
    local.timeZone = [NSTimeZone defaultTimeZone];
    local.alertBody = [NSString stringWithFormat:@"%@ Recuerda que tienes que: %@", [[NSUserDefaults standardUserDefaults] objectForKey:@"user"], [[NSUserDefaults standardUserDefaults] objectForKey:@"selected"]];
    local.fireDate = [self.DatePicker date];
   /* if ([[self.userdefaults objectForKey:@"sound"]isEqualToString:@"sound1"]) {
        local.soundName = @"sound1.caf";
    }else if([[self.userdefaults objectForKey:@"sound"]isEqualToString:@"sound2"]){
        local.soundName = @"sound2.caf";
    }else if([[self.userdefaults objectForKey:@"sound"]isEqualToString:@"sound1"]){
        local.soundName = @"sound3.caf";
    }else{
        local.soundName = @"sound1.caf";
    }*/
    local.soundName = UILocalNotificationDefaultSoundName;


}

- (IBAction)switchChanged:(id)sender {
if(self.NotSwith.isOn == YES){
        [[UIApplication sharedApplication] scheduleLocalNotification:local];


       }
}

Upvotes: 1

Views: 8611

Answers (5)

Jason Campbell
Jason Campbell

Reputation: 543

Make sure you call UNUserNotificationCenter.requestAuthorization() including the .sound option.

That sounds obvious, but is something that might easily go overlooked if your app has a) previously asked for authorization without sounds, b) been updated to add sounds, and c) uses the UNUserNotificationCenter.current().getNotificationSettings() value to decide whether to request permission for notifications.

getNotificationSettings() will return .authorized if you have any Notification authorizations at all. But your app will remain silent if you haven't requested .sounds.

Upvotes: 0

jake
jake

Reputation: 1

In my case the Simulator works fine. The iPad will play music, etc. The problem was: Next to the volume controls on the iPad, the Ringer switch was Off. This stopped the Alert sounds from getting through.

Upvotes: 0

Irfan
Irfan

Reputation: 4331

Make sure that you are cleaning the old notifications with

UIApplication* app = [UIApplication sharedApplication];
NSArray*  oldNotifications = [app scheduledLocalNotifications];

// Clear out the old notification before scheduling a new one.

[app cancelAllLocalNotifications];

in case you don't want to cancel all notifications then try changing the order of badge number and sound name.

Hopefully this will resolve your problem.

Upvotes: 0

Abdul Yasin
Abdul Yasin

Reputation: 3508

Hmm, Interestingly, I changed the order of

notification.SoundName = UILocalNotification.DefaultSoundName;
notification.ApplicationIconBadgeNumber = 1;

to

notification.ApplicationIconBadgeNumber = 1;
notification.SoundName = UILocalNotification.DefaultSoundName;

and it works now. When the app is running in background, the local notification fires and plays the default notification sound.

Upvotes: 2

Femi
Femi

Reputation: 64700

Might be a comparatively simple solution: can you test if it works in the Simulator? If it works in the Simulator and not in the device I'd suggest making sure the mute switch is not on (spent 4 hours yesterday figuring this out).

Upvotes: 5

Related Questions