iOS開(kāi)發(fā)筆記-138: swift-iOS15后昆咽,推送商家收款語(yǔ)音播報(bào)

先添加UNNotificationServiceExtension家妆。File - new - targrt - UNNotificationServiceExtension

項(xiàng)目和UNNotificationServiceExtension钠龙,都要添加appGroups蝗拿,GroupIdentifier要一致,一般是"group.自己項(xiàng)目的BundleIdentifier"凝化,不一致會(huì)有問(wèn)題稍坯。
添加appGroups的原因是:共享一個(gè)group后,可以合成播報(bào)語(yǔ)音搓劫,然后存到group中遵班,之后在UNNotificationServiceExtension中可以讀取到合成后的語(yǔ)音文件祭埂。

UNNotificationServiceExtension的Minimum Deployment最低版本號(hào)要改一下,默認(rèn)是最高iOS系統(tǒng)版本。會(huì)出現(xiàn)低版本真機(jī)調(diào)試不可用的情況

網(wǎng)上的實(shí)現(xiàn)方式有3種旋奢,我用的是第一種炊豪。
關(guān)于音頻的拼接湃缎,如果用Data數(shù)據(jù)流的方式拼接岁忘,會(huì)出現(xiàn)只有第一段音頻聲音的問(wèn)題,所有還是要用AVAssetExportSession來(lái)拼接深员。
1種是修改系統(tǒng)的通知聲音负蠕,可以實(shí)現(xiàn)效果,但是因?yàn)橐袅坎荒苷{(diào)節(jié)倦畅,聲音太小遮糖。如果對(duì)音量沒(méi)有要求的,可以用這個(gè)方案叠赐,而且體驗(yàn)更好欲账。

//自定義通知聲音
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        let keyS = bestAttemptContent?.userInfo["key"] as? String
        if keyS == "sound" {
            let valueS = bestAttemptContent?.userInfo["value"] as? String//金額
            let formatter = NumberFormatter()
            formatter.locale = Locale(identifier: "zh-CN")
            formatter.allowsFloats = true
            let strF: CGFloat = formatter.number(from: valueS ?? "0") as? CGFloat ?? 0.0
            formatter.numberStyle = . spellOut
            let resultS: String = formatter.string(from: NSNumber (value: strF)) ?? ""
            let resultSA: Array<String> = resultS.map { String($0) }
            var finalA: Array<String> = ["錢(qián)響", "哥小兔到賬"]
            finalA.append(contentsOf: resultSA)
            finalA.append("元")
            mergeAVAsset(with: finalA) { (name,path)  in
                if let bestAttemptContent = self.bestAttemptContent {
                    let temp3 = UNNotificationSoundName(rawValue: name ?? "")
                    let temp4 = UNNotificationSound.init(named: temp3)
                    bestAttemptContent.sound = temp4
                    contentHandler(bestAttemptContent)
                }
            }
        } else {
            if let bestAttemptContent = self.bestAttemptContent {
                contentHandler(bestAttemptContent)
            }
        }
    }

第2種是:AVAudioPlayer播放語(yǔ)音,瑕疵點(diǎn)就是芭概,推送橫幅要在語(yǔ)音播報(bào)結(jié)束后出現(xiàn)赛不,不然就會(huì)和AVAudioPlayer語(yǔ)音播報(bào)沖突。(功能實(shí)現(xiàn)了罢洲,但是打包上傳有問(wèn)題踢故,這個(gè)當(dāng)打包上傳到Appstore上就會(huì)出現(xiàn)錯(cuò)誤了,提示說(shuō)UNNotificationServiceExtension中的UIBackgroundModes的Audio這個(gè)字段是非法的惹苗,如果是企業(yè)分發(fā)模式可以添加殿较。要上架審核的話,是無(wú)法添加的)所以這個(gè)方法只適合企業(yè)分發(fā)用鸽粉,上架的話暫時(shí)還是用上面的第1方法斜脂。

UNNotificationServiceExtension 要添加一下權(quán)限抓艳,否則AVAudioPlayer不能在UNNotificationServiceExtension中播放


截屏2024-08-23 10.53.39.png
import UserNotifications
import MediaPlayer

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    var audioPlayer: AVAudioPlayer?
    
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        let keyS = bestAttemptContent?.userInfo["type"] as? Int
        if keyS == 2 {
            let valueS = bestAttemptContent?.userInfo["amount"] as? String//金額
            let formatter = NumberFormatter()
            formatter.locale = Locale(identifier: "zh-CN")
            formatter.allowsFloats = true
            let strF: CGFloat = formatter.number(from: valueS ?? "0") as? CGFloat ?? 0.0
            formatter.numberStyle = . spellOut
            let resultS: String = formatter.string(from: NSNumber (value: strF)) ?? ""
            let resultSA: Array<String> = resultS.map { String($0) }
            var finalA: Array<String> = ["錢(qián)響", "哥小兔到賬"]
            finalA.append(contentsOf: resultSA)
            finalA.append("元")
            mergeAVAsset(with: finalA) { (name,path)  in
//                NSLog("合并后的音頻路徑: \(path)")
                let audioSession = AVAudioSession.sharedInstance()
                do {//配置音頻触机,可以在后臺(tái)、靜音情況下播放
                    try audioSession.setCategory(.playback, mode:.default, options: [.mixWithOthers])
                    try audioSession.setActive(true)
                } catch {
                    print("Error configuring audio session: \(error)")
                }
                self.audioPlayer = try? AVAudioPlayer(contentsOf: path ?? URL(fileURLWithPath: ""))
                self.audioPlayer?.delegate = self
                self.audioPlayer?.play()
            }
        } else {
            if let bestAttemptContent = self.bestAttemptContent {
                // Modify the notification content here...
//                    bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
                contentHandler(bestAttemptContent)
            }
        }
    }
     
    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }
    //合成要播報(bào)的語(yǔ)音
    func mergeAVAsset(with sourcePathArr: [String], rate: Double = 1.0, completed: @escaping (_ soundName: String?,_ soundsFileURL: URL?) -> Void) {
        // 創(chuàng)建音頻軌道, 并獲取多個(gè)音頻素材的軌道
        let composition = AVMutableComposition()
        // 音頻插入的開(kāi)始時(shí)間, 用于記錄每次添加音頻文件的開(kāi)始時(shí)間
        var beginTime = CMTime.zero
        for audioFilePath in sourcePathArr {
            // 獲取音頻素材
            let audioFileURL: String = Bundle.main.path(forResource: audioFilePath, ofType: "mp3") ?? ""
            guard let audioAsset = AVURLAsset(url: URL(fileURLWithPath: audioFileURL)) as AVAsset? else { continue }
            // 音頻軌道
            let audioTrack = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid)
            // 獲取音頻素材軌道
            guard let audioAssetTrack = audioAsset.tracks(withMediaType: .audio).first else { continue }
            // 音頻合并 - 插入音軌文件
            try? audioTrack?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: audioAsset.duration), of: audioAssetTrack, at: beginTime)
            // 記錄尾部時(shí)間
            beginTime = CMTimeAdd(beginTime, audioAsset.duration)
        }
        let finalTimeValue = composition.duration.value * Int64(10 * rate)
        let finalTimeScale = composition.duration.timescale * 10
        composition.scaleTimeRange(.init(start: .zero, end: composition.duration), toDuration: .init(value: finalTimeValue, timescale: finalTimeScale))
        
        let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.自己項(xiàng)目的BundleIdentifier")
        //建立文件夾
        let soundsURL = groupURL?.appendingPathComponent("Library/", isDirectory: true)
        let soundsURL2 = groupURL?.appendingPathComponent("Library/Sounds/", isDirectory: true)
        do {
            //建立文件夾
            if !FileManager.default.fileExists(atPath: (soundsURL?.path ?? "")) {
                try FileManager.default.createDirectory(atPath: (soundsURL?.path ?? ""), withIntermediateDirectories: true, attributes: nil)
            }
            //建立文件夾
            if !FileManager.default.fileExists(atPath: (soundsURL2?.path ?? "")) {
                try FileManager.default.createDirectory(atPath: (soundsURL2?.path ?? ""), withIntermediateDirectories: true, attributes: nil)
            }
        } catch {
            print("Error 建立文件夾: \(error)")
        }
        // 新建文件名,如果存在就刪除舊的
        let soundName = "sound.m4a"
        let outPutFilePath = "Library/Sounds/" + soundName
        let soundsFileURL = groupURL?.appendingPathComponent(outPutFilePath, isDirectory: false)
        
        if FileManager.default.fileExists(atPath: (soundsFileURL?.path ?? "")) {
            do {
                try FileManager.default.removeItem(atPath:(soundsFileURL?.path ?? ""))
            } catch {
                print("Error 新建文件名儡首,如果存在就刪除舊的: \(error)")
            }
        }
        // 導(dǎo)出合并后的音頻文件
        guard let session = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A) else { completed(nil, nil); return }
        // 音頻文件輸出
        session.outputURL = soundsFileURL
        session.outputFileType = AVFileType.m4a // 與上述的`preset`相對(duì)應(yīng)
        session.shouldOptimizeForNetworkUse = true // 優(yōu)化網(wǎng)絡(luò)
        //            session.audioMix = videoAudioMixTools
        session.exportAsynchronously {
            if session.status == .completed {
//                print("合并成功 ----\(soundsFileURL)")
                completed(soundName, soundsFileURL)
            } else {
                // 其他情況, 具體請(qǐng)看這里`AVAssetExportSessionStatus`.
//                print("合并失敗 ----\(session.status.rawValue)")
//                print("合并失敗 ----\(session.error)")
                completed(nil, nil)
            }
        }
    }
}
extension NotificationService: AVAudioPlayerDelegate {
    func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
        //播放結(jié)束片任,執(zhí)行contentHandler,結(jié)束這個(gè)通知
        if (contentHandler != nil) && (bestAttemptContent != nil) {
            bestAttemptContent?.sound = .none//因?yàn)橛姓Z(yǔ)音播報(bào)了蔬胯,這里把系統(tǒng)通知的聲音關(guān)了
            contentHandler!(bestAttemptContent!)
        }
    }
}

像 哥小兔到賬.mp3对供、 元.mp3、 萬(wàn).mp3 這些語(yǔ)音文件氛濒,可以去ai語(yǔ)音合成产场,類(lèi)似的有:百度、科大訊飛舞竿、標(biāo)貝等

語(yǔ)音生成工具:標(biāo)貝悅讀京景、百度、科大訊飛

第三種
經(jīng)過(guò)查找骗奖,大概率支付寶确徙、微信使用的使用voip模式,通過(guò)查找微信與支付寶的ipa执桌,ipa中配置的文件都UIBackgroundModes(后臺(tái)模式)包含voip鄙皇。
所以大概率支付寶與微信都使用的是Voip PushKit實(shí)現(xiàn)的收款的語(yǔ)音播報(bào)功能
之后也會(huì)調(diào)查下Voip PushKit實(shí)現(xiàn)的收款的語(yǔ)音播報(bào)功能,PushKit和極光推送還不太一樣仰挣。持續(xù)關(guān)注中伴逸。。椎木。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末违柏,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子香椎,更是在濱河造成了極大的恐慌漱竖,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,941評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件畜伐,死亡現(xiàn)場(chǎng)離奇詭異馍惹,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)玛界,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)万矾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人慎框,你說(shuō)我怎么就攤上這事良狈。” “怎么了笨枯?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,345評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵薪丁,是天一觀的道長(zhǎng)遇西。 經(jīng)常有香客問(wèn)我,道長(zhǎng)严嗜,這世上最難降的妖魔是什么粱檀? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,851評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮漫玄,結(jié)果婚禮上茄蚯,老公的妹妹穿的比我還像新娘。我一直安慰自己睦优,他們只是感情好渗常,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著汗盘,像睡著了一般凳谦。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上衡未,一...
    開(kāi)封第一講書(shū)人閱讀 51,688評(píng)論 1 305
  • 那天尸执,我揣著相機(jī)與錄音,去河邊找鬼缓醋。 笑死如失,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的送粱。 我是一名探鬼主播褪贵,決...
    沈念sama閱讀 40,414評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼抗俄!你這毒婦竟也來(lái)了脆丁?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,319評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤动雹,失蹤者是張志新(化名)和其女友劉穎槽卫,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體胰蝠,經(jīng)...
    沈念sama閱讀 45,775評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡歼培,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了茸塞。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片躲庄。...
    茶點(diǎn)故事閱讀 40,096評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖钾虐,靈堂內(nèi)的尸體忽然破棺而出噪窘,到底是詐尸還是另有隱情,我是刑警寧澤效扫,帶...
    沈念sama閱讀 35,789評(píng)論 5 346
  • 正文 年R本政府宣布倔监,位于F島的核電站无切,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏丐枉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評(píng)論 3 331
  • 文/蒙蒙 一掘托、第九天 我趴在偏房一處隱蔽的房頂上張望瘦锹。 院中可真熱鬧,春花似錦闪盔、人聲如沸弯院。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,993評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)听绳。三九已至,卻和暖如春异赫,著一層夾襖步出監(jiān)牢的瞬間椅挣,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,107評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工塔拳, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留鼠证,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,308評(píng)論 3 372
  • 正文 我出身青樓靠抑,卻偏偏與公主長(zhǎng)得像量九,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子颂碧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容