今天給大家介紹一下如何在iOS10下實現(xiàn)程序后臺狀態(tài)下杆烁,將接收到推送消息進行語音播報(demo見文章最后)
前言
前段時間在做XX銀行O2O的商戶端的時候其中有一個功能商戶二維碼收款忘朝。行方提的需求是希望跟支付寶收款的功能一樣矫付,在收款之后能夠進行語音播報裂问,說實話一開始還是比較懵逼的不知道該怎么實現(xiàn)画髓。通過各種百度和探索發(fā)現(xiàn)i0S10下的UNNotificationServiceExtension+ AVSpeechSynthesizer實現(xiàn)這一功能怠肋。
之前項目也比較的忙沒有來得及整理洁奈,最近時間稍微寬裕了點伍茄,就來跟大家分享一下是如何實現(xiàn)的順便記錄一下栋盹。
為項目集成推送
集成推送相對來說挺簡單的,目前市面上的三方也挺多的敷矫,各種集成文檔也挺多的例获。這里我就簡單介紹一下如何集成推送。
1.配置推送證書
這個我這里就不說了網(wǎng)上挺多的也挺簡單的
如果還是有同學不是很清楚 點這里
2.到三方平臺創(chuàng)建應用并上傳推送證書
此處需要注意推送的證書的bundleid要一致
3.為工程配置推送
4.設置后臺
5.編寫推送的代碼(這里以個推為例)
代碼挺多的我就不一一粘貼了如果你實在是不想寫下載個demo一頓復制粘貼就行了
個推
極光
6.測試客戶端能否順利接收到推送消息
到三方推送的后臺直接手動推送看能否接收到推送消息
攔截推送消息進行語音播報
新建一個UNNotificationServiceExtension
1曹仗、新建Target
然后下一步為新建的Target取個名字點擊完成
這里需要注意的是這里的bundleID是你的工程名字的bundleID加上.新Target名稱榨汤。
到這里新建Extension就完成了下面就開始寫代碼吧
2、實現(xiàn)播報代碼
打開我們新建的Extension我們發(fā)現(xiàn)有三個文件
NotificationService.h
NotificationService.m
Info.plist
打開NotificationService.m我們會看到系統(tǒng)默認的代碼
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
// Modify the notification content here...
self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
self.contentHandler(self.bestAttemptContent);
}
- (void)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.
self.contentHandler(self.bestAttemptContent);
}
接下來我們就是要往delegate注入我們播報的代碼了
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
// copy發(fā)來的通知怎茫,開始做一些處理
self.bestAttemptContent = [request.content mutableCopy];
// Modify the notification content here...
// self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
NSLog(@"userInfo----->%@",self.bestAttemptContent.userInfo[@"payload"]);
NSData *jsonData = [self.bestAttemptContent.userInfo[@"payload"] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *pushdic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:nil];
//這里我定義的Type 1--表示需要播報的推送 2--無需播報
//Content是需要播報的內(nèi)容
if ([pushdic[@"Type"] isEqualToString:@"1"]) {
//開始語音播報
[self startSpeaking:pushdic[@"Content"]];
}
self.contentHandler(self.bestAttemptContent);
}
- (void)startSpeaking:(NSString *)words{
AVSpeechSynthesizer * synthsizer = [[AVSpeechSynthesizer alloc] init];
synthsizer.delegate = self;
AVSpeechUtterance * utterance = [[AVSpeechUtterance alloc] initWithString:words];//需要轉(zhuǎn)換的文本
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];//國家語言
utterance.rate = 0.6f;//聲速
utterance.volume = 1;
[synthsizer speakUtterance:utterance];
}
到這里我們的基本功能就實現(xiàn)了收壕,再到三方后臺推送試一下。如果沒有播報再回過頭檢查一下是不是哪一步出現(xiàn)了問題
細心的同學可能會發(fā)現(xiàn)有時候播報的聲音大有時候播報的音量小。這里我們還需要優(yōu)化一下蜜宪,也就是我們自己來控制播報的音量虫埂。我找了一下。發(fā)現(xiàn)確實可以設置音量圃验,但是私有API官方不允許使用即使用了也會被拒掉伏。
我找了一下,發(fā)現(xiàn)一個類MPMusicPlayerController损谦。這個類有一個值岖免,volume可以直接設置。接下來我們修改一下代碼
- (void)startSpeaking:(NSString *)words{
AVSpeechSynthesizer * synthsizer = [[AVSpeechSynthesizer alloc] init];
synthsizer.delegate = self;
AVSpeechUtterance * utterance = [[AVSpeechUtterance alloc] initWithString:words];//需要轉(zhuǎn)換的文本
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];//國家語言
utterance.rate = 0.4f;//聲速
utterance.volume = 1;
//修改播放時的音量 根據(jù)自己的需要來定
MPMusicPlayerController* musicController = [MPMusicPlayerController applicationMusicPlayer];
musicController.volume = 0.7;
[synthsizer speakUtterance:utterance];
}
到此本次分享就完成了照捡,如果有哪位小伙伴還不是很明白可以留言或者私信我颅湘。如果小伙伴覺得這里還有什么不妥留下你的建議我們共同探討 ~~
最后留下Demo有需要的可以去下載