问题
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];
}
}
回答1:
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).
回答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.
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/22446332/local-notification-sound-not-playing