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