總的來說啸蜜,實現(xiàn)收款到賬語音合成播報,真的是走了很長的路辈挂,踩過很多的坑衬横。避免更多的人重復踩坑,我便記錄走過哪些路终蒂,掉過哪些坑...
一. 先來說第一種蜂林,不推薦再用。因為蘋果粑粑不會給你審核通過的拇泣。
1.推送就不說了噪叙,我們使用的是極光推送。
2.語音合成使用的是 AVSpeechSynthesizer
- 為了app退到后臺進程不被殺掉挫酿,
target -> capabilities
中開啟了background modes中的audio選項构眯。 - 在語音合成前需要添加下面代碼。
在applicationWillResignActive函數(shù)里
// 先注冊響應后臺控制
UIApplication.shared.beginReceivingRemoteControlEvents()
// 語音合成前需要添加
// 設置并激活音頻會話類別
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .duckOthers)
} catch {
print(error.localizedDescription)
}
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error.localizedDescription)
}
// 語音合成在推送回調里調用的
// 調用API
SpeechManager.shared.speechWeather(with: alert as! String)
// 實現(xiàn)主要代碼
// 自定義語音播報方法
// 此處只例播報一個String的情況
func speechWeather(with weather: String) {
if let _ = speechUtterance {
synthesizer.stopSpeaking(at: .immediate)
}
//播放聲音
let path = Bundle.main.path(forResource: "sound", ofType: "wav")
let pathURL = NSURL(fileURLWithPath: path!)
do {
audioPlayer = try AVAudioPlayer(contentsOf: pathURL as URL)
} catch {
audioPlayer = nil
}
audioPlayer?.delegate = self
audioPlayer?.prepareToPlay()
audioPlayer?.play()
speechUtterance = AVSpeechUtterance(string: weather)
//語音類型
speechUtterance?.rate = 0.5
synthesizer.speak(speechUtterance!)
}
3. 遇到的問題早龟。
1.看似完成了app前臺和后臺的語音播報惫霸,但是app退到后臺手動殺掉進程就gg了。
二. 最重要的是Notification Service Extension(拓展推送服務)芝加。
1. 在項目中創(chuàng)建notificationservice, 選擇 file -> new -> target -> Notification Service Extension
硅卢。
2. 點擊next。
- product name: 拓展項目服務的名稱藏杖。
- bundle identifier: 是原app項目的bundle identifier+拓展項目服務的名稱将塑。(原app的bundle identifier為com.test,拓展項目名稱為.myService蝌麸,則拓展項目的bundle identifier為com.test.myService)
創(chuàng)建完成在scheme中會多一個myService
3.相關配置点寥。
- 1 首先后端要給你個推送測試內容,內容模板如下:
要記住在aps里一定要有"mutable -content"這個字段来吩,alert 這個用字符串就可以敢辩,不用字典。當然字典也行弟疆,后面可以獲取里面字符串也行
-
2 在service工程里info.plist中添加App Transport Security Settings并且添加 Allow Arbitrary Loads為YES戚长。
4.主要代碼如下。
//
// NotificationService.swift
// MyService
//
// Created by JunQiang on 2018/5/24.
// Copyright ? 2018年 多飛. All rights reserved.
//
import UserNotifications
import AVFoundation
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)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
// bestAttemptContent.title = "收款到賬"
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .duckOthers)
} catch {
print(error.localizedDescription)
}
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error.localizedDescription)
}
//播放收金幣聲音
bestAttemptContent.sound = UNNotificationSound.init(named: "sound.wav")
//語音合成
let aVSpeechSynthesizer = AVSpeechSynthesizer()
let aps = bestAttemptContent.userInfo["aps"]
let str = (aps as! NSDictionary)["alert"];
let aVSpeechUtterance = AVSpeechUtterance(string: str as! String)
aVSpeechUtterance.rate = AVSpeechUtteranceDefaultSpeechRate
aVSpeechUtterance.voice = AVSpeechSynthesisVoice.init(language: "zh-CN")
aVSpeechSynthesizer.speak(aVSpeechUtterance)
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)
}
}
}
4. 支持iOS10以上的手機怠苔,修改service中的target -> Deployment Target 選擇10.0同廉。
注意:播放收金幣的音頻時。
一開始考慮的是AVAudioPlayer播放本地音頻。后來發(fā)現(xiàn)只有第一次收到推送是有播放sound.wav恤溶,其他時候是有語音合成未有播放sound.wav乓诽。
//播放聲音
let path = Bundle.main.path(forResource: "sound", ofType: "wav")
let pathURL = NSURL(fileURLWithPath: path!)
do {
audioPlayer = try AVAudioPlayer(contentsOf: pathURL as URL)
} catch {
audioPlayer = nil
}
audioPlayer?.delegate = self
audioPlayer?.prepareToPlay()
audioPlayer?.play()
經(jīng)過一番搜索帜羊,才發(fā)現(xiàn)bestAttemptContent本身是可以播放音頻的咒程。改完發(fā)現(xiàn)可以了
//播放收金幣聲音
bestAttemptContent.sound = UNNotificationSound.init(named: "sound.wav")