??近期產(chǎn)品大大突然提出一個(gè)蛋疼的需求掌敬,并表示這是“客戶和市場(chǎng)強(qiáng)制要求”做的正卧。蠢熄。。沒辦法炉旷,只能硬著頭皮上了签孔。
??之前采用的是靜默推送+普通推送,而且網(wǎng)上大部分資料也默認(rèn)使用的是靜默推送方式窘行。據(jù)我所知饥追,該方案有一定的缺陷:1、部分機(jī)器收不到語音播報(bào)罐盔;2判耕、若需要在后臺(tái)或鎖屏狀態(tài)下播報(bào)語音時(shí),上架會(huì)遇到"Audio,AirPlay,and Picture in Picture"和"Background fetch"等問題審核被拒翘骂。通過查閱網(wǎng)上的資料,我改用iOS 10新的API推送擴(kuò)展(UNNotificationServiceExtension)帚豪,三方SDK使用極光碳竟,關(guān)于集成方式就不多說了,詳見極光官方文檔狸臣。
??這種方案的特點(diǎn)是:節(jié)省大量代碼莹桅,上架簡單,服務(wù)器只需要推一條消息烛亦,但iOS 10以上"mutable-content = 1"必傳诈泼,否則程序不走這個(gè)擴(kuò)展的Target。
實(shí)現(xiàn)原理
-
iOS 10以上采用推送擴(kuò)展(極光必傳mutable-content煤禽,iOS 10以下不需要)處理铐达,使用iOS原生API AVSpeechSynthesizer實(shí)現(xiàn)文字合成語音。
iOS10以下采用固定音頻文件播放檬果,比如“你有一筆收款”瓮孙。
相關(guān)代碼
- iOS 10以下:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
// iOS 10之前前臺(tái)沒有通知欄
if ([UIDevice currentDevice].systemVersion.floatValue < 10.0 && [UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
// iOS 10以下 極光前臺(tái)不展示消息欄唐断,此處為自定義內(nèi)容
[EBBannerView showWithContent:@"交易結(jié)果通知"];
// 獲取共享域的偏好設(shè)置(可百度“多target數(shù)據(jù)共享”)
NSUserDefaults *userDefault = [[NSUserDefaults alloc] initWithSuiteName:@"group.xxx"];
BOOL canSound = [userDefault boolForKey:@"voice"];
if (canSound) {
// 播放refund.wav或collection.wav固定音頻文件
if ([refund condition]) {
[self playMusic:@"refund" type:@"wav"];
} else {
[self playMusic:@"collection" type:@"wav"];
}
}
}
}
// 播放音頻文件
- (void)playMusic:(NSString *)name type:(NSString *)type {
//得到音效文件的地址
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:name ofType:type];
//將地址字符串轉(zhuǎn)換成url
NSURL *soundURL = [NSURL fileURLWithPath:soundFilePath];
//生成系統(tǒng)音效id
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &_soundFileObject);
//播放系統(tǒng)音效
AudioServicesPlaySystemSound(_soundFileObject);
}
- iOS 10及以上:
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
// iOS 10在有語音播報(bào)的情況下 可屏蔽系統(tǒng)提示音,也可根據(jù)需求來
self.bestAttemptContent.sound = nil;
// Modify the notification content here...
// 獲取共享域的偏好設(shè)置
NSUserDefaults *userDefault = [[NSUserDefaults alloc] initWithSuiteName:@"group. xxx"];
// 解析推送自定義參數(shù)userInfo
NSDictionary *userInfo = [self dictionaryWithUserInfo:self.bestAttemptContent.userInfo];
BOOL canSound = [userDefault boolForKey:@"voice"];
NSString *voiceString = nil;
if (canSound) {
if ([refund condition]) {
voiceString = [NSString stringWithFormat:@"退款%@元杭抠!", userInfo[@"money"]];
} else {
voiceString = [NSString stringWithFormat:@"收款%@元脸甘!", userInfo[@"money"]];
}
}
// 語音合成
[self syntheticVoice:voiceString];
self.contentHandler(self.bestAttemptContent);
}
- (void)syntheticVoice:(NSString *)string {
// 語音合成
self.synthesizer = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *speechUtterance = [AVSpeechUtterance speechUtteranceWithString:string];
//設(shè)置語言類別(不能被識(shí)別,返回值為nil)
speechUtterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
//設(shè)置語速快慢
speechUtterance.rate = 0.55;
//語音合成器會(huì)生成音頻
[self.synthesizer speakUtterance:speechUtterance];
}