問題
iOS12.1之后利用本地推送實現(xiàn)消息的語音播報,在iOS15 沒有聲音。
iOS15版本下,配置UNNotificationSound來替換UNNotificationRequest聲音,會出現(xiàn)沒法播報問題
原因
iOS15本地推送新增了中斷級別屬性 interruptionLevel,對通知進行了分級 。而且通知的內容不能為空。
解決方案
使用非Passive的中斷級別進行本地通知才會有聲音,且本地推送一定要有內容,即body不能為空。content.body = @" 不能為空";
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; //標題
content.sound = [UNNotificationSound soundNamed:[NSString stringWithFormat:@"%@.mp3",mp3Name]];
content.body = @"語音播報";// 本地推送一定要有內容,即body不能為空。
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 150000
if (@available(iOS 15.0, *)) {
content.interruptionLevel = UNNotificationInterruptionLevelTimeSensitive;//會使手機亮屏且會播放聲音;可能會在免打擾模式(焦點模式)下展示
// @"{\"aps\":{\"interruption-level\":\"time-sensitive\"}}";
// @"{\"aps\":{\"interruption-level\":\"active\"}}";
content.body = @"語音播報";// 本地推送一定要有內容,即body不能為空。
}
#endif
// repeats,是否重復,如果重復的話時間必須大于60s,要不會報錯
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.01 repeats:NO];
/* */
//添加通知的標識符,可以用于移除,更新等搡作
NSString * identifier = [[NSUUID UUID] UUIDString];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {
}];