iOS10 實(shí)現(xiàn)支付寶收款播報(bào)

今天給大家介紹一下如何在iOS10下實(shí)現(xiàn)程序后臺(tái)狀態(tài)下,將接收到推送消息進(jìn)行語(yǔ)音播報(bào)(demo見(jiàn)文章最后)

前言

前段時(shí)間在做XX銀行O2O的商戶(hù)端的時(shí)候其中有一個(gè)功能商戶(hù)二維碼收款。行方提的需求是希望跟支付寶收款的功能一樣,在收款之后能夠進(jìn)行語(yǔ)音播報(bào),說(shuō)實(shí)話(huà)一開(kāi)始還是比較懵逼的不知道該怎么實(shí)現(xiàn)。通過(guò)各種百度和探索發(fā)現(xiàn)i0S10下的UNNotificationServiceExtension+ AVSpeechSynthesizer實(shí)現(xiàn)這一功能。

之前項(xiàng)目也比較的忙沒(méi)有來(lái)得及整理,最近時(shí)間稍微寬裕了點(diǎn),就來(lái)跟大家分享一下是如何實(shí)現(xiàn)的順便記錄一下。

為項(xiàng)目集成推送

集成推送相對(duì)來(lái)說(shuō)挺簡(jiǎn)單的,目前市面上的三方也挺多的,各種集成文檔也挺多的。這里我就簡(jiǎn)單介紹一下如何集成推送。

1.配置推送證書(shū)

這個(gè)我這里就不說(shuō)了網(wǎng)上挺多的也挺簡(jiǎn)單的
如果還是有同學(xué)不是很清楚 點(diǎn)這里

2.到三方平臺(tái)創(chuàng)建應(yīng)用并上傳推送證書(shū)

此處需要注意推送的證書(shū)的bundleid要一致

3.為工程配置推送
開(kāi)啟推送.png
4.設(shè)置后臺(tái)
設(shè)置后臺(tái).png
5.編寫(xiě)推送的代碼(這里以個(gè)推為例)

代碼挺多的我就不一一粘貼了如果你實(shí)在是不想寫(xiě)下載個(gè)demo一頓復(fù)制粘貼就行了
個(gè)推
極光

6.測(cè)試客戶(hù)端能否順利接收到推送消息

到三方推送的后臺(tái)直接手動(dòng)推送看能否接收到推送消息

攔截推送消息進(jìn)行語(yǔ)音播報(bào)

新建一個(gè)UNNotificationServiceExtension

1、新建Target
屏幕快照 2017-10-13 下午3.22.13.png

新建Target.png

然后下一步為新建的Target取個(gè)名字點(diǎn)擊完成

這里需要注意的是這里的bundleID是你的工程名字的bundleID加上.新Target名稱(chēng)。

到這里新建Extension就完成了下面就開(kāi)始寫(xiě)代碼吧

2、實(shí)現(xiàn)播報(bào)代碼

打開(kāi)我們新建的Extension我們發(fā)現(xiàn)有三個(gè)文件

NotificationService.h
NotificationService.m
Info.plist

打開(kāi)NotificationService.m我們會(huì)看到系統(tǒng)默認(rèn)的代碼

- (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);
}

接下來(lái)我們就是要往delegate注入我們播報(bào)的代碼了

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    // copy發(fā)來(lái)的通知,開(kāi)始做一些處理
    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--表示需要播報(bào)的推送 2--無(wú)需播報(bào)
    //Content是需要播報(bào)的內(nèi)容
    if ([pushdic[@"Type"] isEqualToString:@"1"]) {
         //開(kāi)始語(yǔ)音播報(bào)
        [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"];//國(guó)家語(yǔ)言
    utterance.rate = 0.6f;//聲速
    utterance.volume = 1;
    [synthsizer speakUtterance:utterance];
}

到這里我們的基本功能就實(shí)現(xiàn)了,再到三方后臺(tái)推送試一下。如果沒(méi)有播報(bào)再回過(guò)頭檢查一下是不是哪一步出現(xiàn)了問(wèn)題

細(xì)心的同學(xué)可能會(huì)發(fā)現(xiàn)有時(shí)候播報(bào)的聲音大有時(shí)候播報(bào)的音量小。這里我們還需要優(yōu)化一下,也就是我們自己來(lái)控制播報(bào)的音量。我找了一下。發(fā)現(xiàn)確實(shí)可以設(shè)置音量,但是私有API官方不允許使用即使用了也會(huì)被拒。
我找了一下,發(fā)現(xiàn)一個(gè)類(lèi)MPMusicPlayerController。這個(gè)類(lèi)有一個(gè)值,volume可以直接設(shè)置。接下來(lái)我們修改一下代碼

- (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"];//國(guó)家語(yǔ)言
    utterance.rate = 0.4f;//聲速
    utterance.volume = 1;
    //修改播放時(shí)的音量 根據(jù)自己的需要來(lái)定
    MPMusicPlayerController* musicController = [MPMusicPlayerController applicationMusicPlayer];
    musicController.volume = 0.7;
    [synthsizer speakUtterance:utterance];
}

到此本次分享就完成了,如果有哪位小伙伴還不是很明白可以留言或者私信我。如果小伙伴覺(jué)得這里還有什么不妥留下你的建議我們共同探討 ~~
最后留下Demo有需要的可以去下載

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容