简体   繁体   English

本地通知声音没有播放?

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


       }
}

Might be a comparatively simple solution: can you test if it works in the Simulator? 可能是一个相对简单的解决方案:您可以测试它是否可以在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). 如果它在模拟器中而不在设备中有效,我建议确保静音开关未打开(昨天花了4个小时弄清楚了这一点)。

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. 当应用在后台运行时,将触发本地通知并播放默认的通知声音。

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. 希望这可以解决您的问题。

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. iPad将播放音乐等。问题是:在iPad上的音量控件旁边,“ 铃声”开关处于关闭状态。 This stopped the Alert sounds from getting through. 这阻止了警报声音的通过。

Make sure you call UNUserNotificationCenter.requestAuthorization() including the .sound option.确保调用UNUserNotificationCenter.requestAuthorization()包括.sound选项。

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.这听起来很明显,但是如果您的应用程序有 a) 之前要求授权但没有声音,b) 已更新以添加声音,并且 c) 使用UNUserNotificationCenter.current().getNotificationSettings()值来决定,那么这可能很容易被忽略是否请求通知权限。

getNotificationSettings() will return .authorized if you have any Notification authorizations at all .如果您有任何通知授权, getNotificationSettings()将返回.authorized But your app will remain silent if you haven't requested .sounds .但是如果您没有请求.sounds ,您的应用程序将保持静默。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM