?最近公司的一個(gè)新項(xiàng)目要用到推送,然后語音播報(bào)推送里的內(nèi)容蓄愁。比如支付寶的推送功能“支付寶到賬100元”旁趟,這種的蹭越。
?目前做這個(gè)的方法第一個(gè)想到的就是Notification Service Extension了,Notification Service Extension是ios10推出的新功能弛槐,所以只能在ios10及其以上的手機(jī)上有用懊亡。
Notification Service Extension的作用就是在蘋果服務(wù)器在給手機(jī)推送消息的中間,進(jìn)入Notification Service Extension丐黄, 我們可以在進(jìn)Notification Service Extension時(shí)對(duì)推送的消息 進(jìn)行一次加工斋配,這就是Notification Service Extension的功能的作用。
首先你要確定你已經(jīng)接好了推送灌闺,可以收到推送了艰争!
現(xiàn)在我們對(duì)Notification Service Extension進(jìn)行創(chuàng)建:
這里說的不準(zhǔn)確,Content Extension ?是用來自定義推送UI和一些附加功能的 桂对,這么說比較好理解甩卓。
第二步,有自定義需求的可以吧兩個(gè)都創(chuàng)建出來蕉斜,不需要的只用創(chuàng)建Notification Service Extension就可以了逾柿。
第三步缀棍,創(chuàng)建成功之后,就會(huì)在項(xiàng)目中出現(xiàn)新創(chuàng)建的文件夾机错,里面有兩個(gè)文件爬范,
這里要多說一句,創(chuàng)建成功之后弱匪,我們的Notification Service Extension的 Bundle Identifier應(yīng)該是青瀑,原本的項(xiàng)目BundleIdentifier.XXX(Notification Service Extension的id),即原項(xiàng)目的Bundle Identifier.你的項(xiàng)目名萧诫,這是一個(gè)固定格式 不能出錯(cuò)斥难。
第四部,創(chuàng)建好后帘饶,.m文件中 有兩個(gè)方法哑诊,
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void(^)(UNNotificationContent *_Nonnull))contentHandler {}
- (void)serviceExtensionTimeWillExpire {}
我們?cè)诘谝粋€(gè)方法里對(duì)通知進(jìn)行處理,第二個(gè)方法是緊急方法及刻,即第一個(gè)方法處理不完的時(shí)候就在第二個(gè)方法緊急處理镀裤,這個(gè)方法我還不是很了解具體的作用。
大家可以用下面這段代碼測(cè)試一下提茁,這里改變了通知的大標(biāo)題淹禾,小標(biāo)題,和內(nèi)容:
- (void)didReceiveNotificationRequest:(UNNotificationRequest*)request withContentHandler:(void(^)(UNNotificationContent*_Nonnull))contentHandler {
? ? self.contentHandler= contentHandler;
? ? self.bestAttemptContent= [request.contentmutableCopy];
? ? // Modify the notification content here...
//? ? self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
? ? // 重寫一些東西
? ? self.bestAttemptContent.title = @"我是大標(biāo)題";
? ? self.bestAttemptContent.subtitle = @"我是小標(biāo)題";
? ? self.bestAttemptContent.body =@"我是內(nèi)容";
}
接下來就是簡單的語音合成了茴扁,
#import <AVFoundation/AVFoundation.h>
@property?(nonatomic,?strong)AVSpeechSynthesizer *synthesizer;
- (void)didReceiveNotificationRequest:(UNNotificationRequest*)request withContentHandler:(void(^)(UNNotificationContent*_Nonnull))contentHandler {
? ? self.contentHandler= contentHandler;
? ? self.bestAttemptContent= [request.contentmutableCopy];
? ? // Modify the notification content here...
//? ? self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
? ? // 重寫一些東西
? ? NSDictionary*dict =? self.bestAttemptContent.userInfo;
? ? NSDictionary*notiDict = dict[@"aps"];
? ? NSString*content = [notiDictvalueForKey:@"alert"];//這里取到我們通知的內(nèi)容铃岔,字段和后臺(tái)提前溝通好。
? ? NSString*voiceString =nil;
? ? voiceString = [NSStringstringWithFormat:@"%@", content];
? ? [selfsyntheticVoice:voiceString];
? ? self.contentHandler(self.bestAttemptContent);
}
- (void)syntheticVoice:(NSString*)string {
? ??//? 語音合成
? ? self.synthesizer = [[AVSpeechSynthesizer alloc] init];
? ? AVSpeechUtterance*speechUtterance = [AVSpeechUtterancespeechUtteranceWithString:string];
? ? //設(shè)置語言類別(不能被識(shí)別峭火,返回值為nil)
? ? speechUtterance.voice= [AVSpeechSynthesisVoicevoiceWithLanguage:@"zh-CN"];
? ? //設(shè)置語速快慢
? ? speechUtterance.rate=0.5;//0.5是一個(gè)
? ? //語音合成器會(huì)生成音頻
? ? [self.synthesizerspeakUtterance:speechUtterance];
}
這就是最簡單的語音合成通知內(nèi)容并播放了毁习。
最后就是運(yùn)行了:
后面就是你可能會(huì)用到的tip了:
1.
2.我用的極光作為例子,這里自己測(cè)試的時(shí)候的選項(xiàng)卖丸,注意:這里很關(guān)鍵纺且,一定要有"mutable-content": "1"這個(gè)字段,不然擴(kuò)展是攔截不到你的推送的稍浆, 這個(gè)字段也要和推送內(nèi)容字段在極光是Alert字段 在 同一級(jí)载碌。這個(gè)要給后臺(tái)說清楚不然攔截不到。
3.運(yùn)行推送擴(kuò)展你的斷點(diǎn)不會(huì)起作用衅枫。
4.在我連線測(cè)試的時(shí)候嫁艇,攔截到推送后就會(huì)崩潰,但是不連線測(cè)試就沒問題弦撩,這個(gè)我也沒找到原因步咪。
5.不論你的調(diào)試是release還是debug,只要是連線運(yùn)行上的項(xiàng)目都只能收到開發(fā)狀態(tài)下的推送益楼, ?想收到生產(chǎn)狀態(tài)下的推送 就需要到appstore猾漫,或者打測(cè)試包分發(fā)出來点晴。