[英]Local Notification sound not playing?
我在本地通知中遇到了这个问题,它们没有播放声音。 我用默认的和自定义的进行了测试,这显然是我想玩但没有的。 我看了很多人都面临这个问题,但他们的解决方案都不适合我。
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];
}
}
可能是一个相对简单的解决方案:您可以测试它是否可以在Simulator中运行吗? 如果它在模拟器中而不在设备中有效,我建议确保静音开关未打开(昨天花了4个小时弄清楚了这一点)。
嗯,有趣的是,我改变了
notification.SoundName = UILocalNotification.DefaultSoundName;
notification.ApplicationIconBadgeNumber = 1;
至
notification.ApplicationIconBadgeNumber = 1;
notification.SoundName = UILocalNotification.DefaultSoundName;
现在就可以了。 当应用在后台运行时,将触发本地通知并播放默认的通知声音。
确保您正在清理旧的通知
UIApplication* app = [UIApplication sharedApplication];
NSArray* oldNotifications = [app scheduledLocalNotifications];
//在安排新通知之前清除旧通知。
[app cancelAllLocalNotifications];
如果您不想取消所有通知,请尝试更改徽章编号和声音名称的顺序。
希望这可以解决您的问题。
就我而言,模拟器工作正常。 iPad将播放音乐等。问题是:在iPad上的音量控件旁边,“ 铃声”开关处于关闭状态。 这阻止了警报声音的通过。
确保调用UNUserNotificationCenter.requestAuthorization()
包括.sound
选项。
这听起来很明显,但是如果您的应用程序有 a) 之前要求授权但没有声音,b) 已更新以添加声音,并且 c) 使用UNUserNotificationCenter.current().getNotificationSettings()
值来决定,那么这可能很容易被忽略是否请求通知权限。
如果您有任何通知授权, getNotificationSettings()
将返回.authorized
。 但是如果您没有请求.sounds
,您的应用程序将保持静默。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.