iOS15 發(fā)送本地通知(附帶聲音)無法播報

問題

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) {

    }];
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容