由于現(xiàn)在二維碼支付越來越流行。越來越多的小伙伴參與到聚合支付的行業(yè)中。那在app的開發(fā)中,金額播報這個功能肯定是必要的一項了!
下面直接進入主題
金額播報的情形(主流app工具如“支付寶”“收錢吧”)
1、iOS 10以下的設備收到錢之后不管App是殺死還是進入后臺狀態(tài)都會播報”xxxx收款成功”一句固定的語音
2、iOS 10以上的設備,收到錢之后,不管APP是殺死還是壓入后臺狀態(tài),在收到轉賬的時候,會播報”xxx到賬 xxx 元”
實現(xiàn)以上功能注意的點:
iOS 10以上和iOS10以下設備,實現(xiàn)方式不一樣
1.iOS10以前的收款播報是在后臺或者app被殺死的時候,播放一個固定的聲音,可以借助遠程推送定制鈴聲的功能來實現(xiàn),只要在本地添加一段提前錄制好的語音,并且在推送內容的時候將sound字段,修改成語音的名稱即可。(ios 10 之前也可以有另類的方法做到,后臺金額播報,后面會有介紹)
2.iOS10以后,無論是app在后臺,或者沒有開啟,都可以進行具體的播報金額. 用到的是iOS 10的推送擴展( Notification Extension)
推薦的語音播報的兩種方式
1.科大訊飛的語音合成 ?(有兩個GG 兩個MM的聲音可以選) ? ? ? ? ? ? ? ? ? ? ? ?-> ? ? ?科大訊飛語音合成
2.系統(tǒng)語音合成? ? ? ? ?->?系統(tǒng)語音播報
#import <AVFoundation/AVFoundation.h>
AVSpeechUtterance * aVSpeechUtterance = [[AVSpeechUtterance alloc] initWithString:@"想說啥就說啥"];
aVSpeechUtterance.rate = AVSpeechUtteranceDefaultSpeechRate;?
aVSpeechUtterance.voice =[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];?
[self.aVSpeechSynthesizer speakUtterance:aVSpeechUtterance];?
下面我們一步一步來完成這個功能的實現(xiàn)
iOS 10以上處理方法 Notification Extension ?
原理如下,想要深入研究的可以看看 ? ?NotificationServiceExtension
1.
生成以下的文件
點開的.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 可以獲取所有推送信息,里面可以取得播報內容
AVSpeechUtterance * aVSpeechUtterance = [[AVSpeechUtterance alloc] initWithString:@"這里放入播報的聲音文字就行了"];
aVSpeechUtterance.rate = AVSpeechUtteranceDefaultSpeechRate; ??
aVSpeechUtterance.voice =[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]; ??
[self.aVSpeechSynthesizer speakUtterance:aVSpeechUtterance];
self.contentHandler(self.bestAttemptContent);
}
你以為這就結束了嗎???NO,NO,NO 下面很關鍵
1. 后端給你推送過來的json 必須添加 ? ? "mutable-content":1 ? ? (后臺一直引用不出來這個參數(shù)方法,請讓他升級推送!)
2.播放的語音時長最好不要超過30秒 ? (我也不信誰能播放30秒)
3.iOS10以下推送的sound 有值,想要iOS10 以上沒有推送聲音 可以設置 self.bestAttemptContent.sound = nil;
4.斷點調試的時候,跑正確的target
iOS10以上推送金額播報大致就是這樣的一個流程。本文章也只提供一個大致的思路和實現(xiàn)方法。具體的細節(jié)處理 可以自己去琢磨。比如支付寶的金額播報,他們的錢的單位和0 - 9 的數(shù)字都是固定語音,大概就是將推送獲取的金額,然后音頻拼接起來,有興趣的可以自己研究。
下面簡單說說iOS10以下的后臺金額播報
首先,未開啟app的話不可能詳細的金額播報啦!!
方式一 ,app進入后臺后 就進行無聲的音樂播放,每三分鐘撥一次,保證app一直后臺活躍,并且 ?
然后接到推送后,就可以播報
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {
//回調
NSLog(@"Received remote notification with userInfo %@", userInfo);
completionHandler(UIBackgroundFetchResultNewData);
//語音播報
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@“userinfo有你要讀的東西”];
AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
[synth speakUtterance:utterance];
}
重點:
1."content-avilable" =1 ? 后端必須傳這個字段。 (靜默推送) ? ?
2.
勾選后,可能會被蘋果拒絕!你可以上傳一段你使用后臺播放的用途!這個通過率大概30% ,看運氣!
如果你有更好的建議,想法,或者疑問,歡迎留言!