1

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];


       }
}
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
Karlo A. López
  • 2,548
  • 3
  • 29
  • 56
  • You forgot the `;` in the first line of code. Should be `UILocalNotification *local;` not `UILocalNotification *local` – wigging Mar 17 '14 at 03:21
  • 1
    Sorry I put that when I was posting, obviously in my project its well, otherwise it will not even run, the problem is when the notification is shown in the device in does not sound. – Karlo A. López Mar 17 '14 at 03:26
  • What state is your app in when you are sending the notification? Keep in mind that if the application is not frontmost and visible, the system displays the alert message, badges the application, and plays a sound whatever is specified in the notification. If the application is foremost and visible when the system delivers the notification, no alert is shown, no icon is badged, and no sound is played. – wigging Mar 17 '14 at 03:29
  • The notification is scheduled when activating a switch, is active... – Karlo A. López Mar 17 '14 at 03:31

5 Answers5

6

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).

Femi
  • 64,273
  • 8
  • 118
  • 148
  • Mm no it also dont work in simulator, In the devices I get twice shure that the mute was off... Im getting crazy about this! haha – Karlo A. López Mar 17 '14 at 04:05
2

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.

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Abdul Yasin
  • 3,480
  • 1
  • 28
  • 42
0

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.

Abdul Yasin
  • 3,480
  • 1
  • 28
  • 42
Irfan
  • 4,301
  • 6
  • 29
  • 46
0

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.

jake
  • 1
0

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.

Jason Campbell
  • 483
  • 4
  • 12