由于現(xiàn)在二維碼支付越來(lái)越流行。越來(lái)越多的小伙伴參與到聚合支付的行業(yè)中。那在app的開(kāi)發(fā)中,金額播報(bào)這個(gè)功能肯定是必要的一項(xiàng)了!
下面直接進(jìn)入主題
金額播報(bào)的情形(主流app工具如“支付寶”“收錢(qián)吧”)
1、iOS 10以下的設(shè)備收到錢(qián)之后不管App是殺死還是進(jìn)入后臺(tái)狀態(tài)都會(huì)播報(bào)”xxxx收款成功”一句固定的語(yǔ)音
2、iOS 10以上的設(shè)備,收到錢(qián)之后,不管APP是殺死還是壓入后臺(tái)狀態(tài),在收到轉(zhuǎn)賬的時(shí)候,會(huì)播報(bào)”xxx到賬 xxx 元”
實(shí)現(xiàn)以上功能注意的點(diǎn):
iOS 10以上和iOS10以下設(shè)備,實(shí)現(xiàn)方式不一樣
1.iOS10以前的收款播報(bào)是在后臺(tái)或者app被殺死的時(shí)候,播放一個(gè)固定的聲音,可以借助遠(yuǎn)程推送定制鈴聲的功能來(lái)實(shí)現(xiàn),只要在本地添加一段提前錄制好的語(yǔ)音,并且在推送內(nèi)容的時(shí)候?qū)ound字段,修改成語(yǔ)音的名稱即可。(ios 10 之前也可以有另類的方法做到,后臺(tái)金額播報(bào),后面會(huì)有介紹)
2.iOS10以后,無(wú)論是app在后臺(tái),或者沒(méi)有開(kāi)啟,都可以進(jìn)行具體的播報(bào)金額. 用到的是iOS 10的推送擴(kuò)展( Notification Extension)
推薦的語(yǔ)音播報(bào)的兩種方式
1.科大訊飛的語(yǔ)音合成 ?(有兩個(gè)GG 兩個(gè)MM的聲音可以選) ? ? ? ? ? ? ? ? ? ? ? ?-> ? ? ?科大訊飛語(yǔ)音合成
2.系統(tǒng)語(yǔ)音合成? ? ? ? ?->?系統(tǒng)語(yǔ)音播報(bào)
#import <AVFoundation/AVFoundation.h>
AVSpeechUtterance * aVSpeechUtterance = [[AVSpeechUtterance alloc] initWithString:@"想說(shuō)啥就說(shuō)啥"];
aVSpeechUtterance.rate = AVSpeechUtteranceDefaultSpeechRate;?
aVSpeechUtterance.voice =[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];?
[self.aVSpeechSynthesizer speakUtterance:aVSpeechUtterance];?
下面我們一步一步來(lái)完成這個(gè)功能的實(shí)現(xiàn)
iOS 10以上處理方法 Notification Extension ?
原理如下,想要深入研究的可以看看 ? ?NotificationServiceExtension

1.


生成以下的文件

點(diǎn)開(kāi)的.m文件
#import "NotificationService.h"
#import@interface NotificationService ()
@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
@property (nonatomic, strong) AVSpeechSynthesizer *aVSpeechSynthesizer;
@end
@implementation NotificationService
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
self.bestAttemptContent.title = [NSString stringWithFormat:@"%@",request.content.body];
self.bestAttemptContent.subtitle = @"";
self.bestAttemptContent.body = @"";
self.aVSpeechSynthesizer = [[AVSpeechSynthesizer alloc] init];
//request 可以獲取所有推送信息,里面可以取得播報(bào)內(nèi)容
AVSpeechUtterance * aVSpeechUtterance = [[AVSpeechUtterance alloc] initWithString:@"這里放入播報(bào)的聲音文字就行了"];
aVSpeechUtterance.rate = AVSpeechUtteranceDefaultSpeechRate; ??
aVSpeechUtterance.voice =[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]; ??
[self.aVSpeechSynthesizer speakUtterance:aVSpeechUtterance];
self.contentHandler(self.bestAttemptContent);
}
你以為這就結(jié)束了嗎???NO,NO,NO 下面很關(guān)鍵
1. 后端給你推送過(guò)來(lái)的json 必須添加 ? ? "mutable-content":1 ? ? (后臺(tái)一直引用不出來(lái)這個(gè)參數(shù)方法,請(qǐng)讓他升級(jí)推送!)
2.播放的語(yǔ)音時(shí)長(zhǎng)最好不要超過(guò)30秒 ? (我也不信誰(shuí)能播放30秒)
3.iOS10以下推送的sound 有值,想要iOS10 以上沒(méi)有推送聲音 可以設(shè)置 self.bestAttemptContent.sound = nil;
4.斷點(diǎn)調(diào)試的時(shí)候,跑正確的target

iOS10以上推送金額播報(bào)大致就是這樣的一個(gè)流程。本文章也只提供一個(gè)大致的思路和實(shí)現(xiàn)方法。具體的細(xì)節(jié)處理 可以自己去琢磨。比如支付寶的金額播報(bào),他們的錢(qián)的單位和0 - 9 的數(shù)字都是固定語(yǔ)音,大概就是將推送獲取的金額,然后音頻拼接起來(lái),有興趣的可以自己研究。
下面簡(jiǎn)單說(shuō)說(shuō)iOS10以下的后臺(tái)金額播報(bào)
首先,未開(kāi)啟app的話不可能詳細(xì)的金額播報(bào)啦!!
方式一 ,app進(jìn)入后臺(tái)后 就進(jìn)行無(wú)聲的音樂(lè)播放,每三分鐘撥一次,保證app一直后臺(tái)活躍,并且 ?

然后接到推送后,就可以播報(bào)
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {
//回調(diào)
NSLog(@"Received remote notification with userInfo %@", userInfo);
completionHandler(UIBackgroundFetchResultNewData);
//語(yǔ)音播報(bào)
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@“userinfo有你要讀的東西”];
AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
[synth speakUtterance:utterance];
}
重點(diǎn):
1."content-avilable" =1 ? 后端必須傳這個(gè)字段。 (靜默推送) ? ?
2.

勾選后,可能會(huì)被蘋(píng)果拒絕!你可以上傳一段你使用后臺(tái)播放的用途!這個(gè)通過(guò)率大概30% ,看運(yùn)氣!
如果你有更好的建議,想法,或者疑問(wèn),歡迎留言!