先說下自己做的項(xiàng)目需求:
其實(shí)需求比較簡(jiǎn)單,就是app在收款到賬時(shí)候能夠像支付寶嘹屯、微信一樣能夠有語(yǔ)音提示:支付寶收款到賬XX元
做出這個(gè)功能很簡(jiǎn)單蔼囊,而且方案也比較多漓雅,但是遇到的問題很多,最大的難處就是上架到App Store時(shí)候一直被拒絕
然后是參考文章鏈接
文章1:http://www.reibang.com/p/c06133d576e4
文章2:http://www.cnblogs.com/bigant9527/p/6144292.html?utm_source=tuicool&utm_medium=referral
方案一:按照參考文章1的方式(注意:后臺(tái)推送一定要加以下字段)
"content-available" = 1
在收到推送消息的方法里面使用AVFoundation 中的AVSpeechSynthesizer來合成語(yǔ)音播報(bào)就可以了
問題:
1.功能問題:
這種方式只能在程序運(yùn)行在前臺(tái)耕渴、在后臺(tái)時(shí)候播報(bào)拘悦,程序退出后不會(huì)語(yǔ)音播報(bào)
2.上架問題:
而且必須在Info.plist中的Background Modes中勾選Audio,AirPlay,and Picture in Picture
這樣上架時(shí)候蘋果會(huì)認(rèn)為你的應(yīng)用并不是語(yǔ)音類(如音樂播放器),所以會(huì)被拒絕
【我看到不少項(xiàng)目是已經(jīng)在Info.plist中的Background Modes勾選過Audio相關(guān)橱脸,如果功能也能滿足的話基本可以O(shè)K】
方案二:使用VoIP Push實(shí)現(xiàn)
需要在開發(fā)者賬號(hào)里面去用制作VoIP Push證書础米,VoIP Push與普通的推送證書不同,可以直接百度搜一下
問題:
1.功能問題:
程序運(yùn)行在前臺(tái)添诉、在后臺(tái)時(shí)候播報(bào)屁桑,程序退出后會(huì)不會(huì)語(yǔ)音播報(bào)我還沒有測(cè)試
2.上架問題:
而且必須在Info.plist中的Background Modes中添加VoIP Push
同樣會(huì)在上架時(shí)候被拒
方案三:使用Notification Service Extension實(shí)現(xiàn)
推薦使用,實(shí)現(xiàn)方案可以直接百度搜到很多
但是在此也遇到了一個(gè)坑就是在Notification Service Extension中的方法中打斷點(diǎn)調(diào)試栏赴,在應(yīng)用收到推送消息時(shí)候好像斷點(diǎn)并不生效蘑斧,而實(shí)際上方法是通的,也就是說能夠語(yǔ)音播報(bào)
#import "NotificationService.h"
#import <AVFoundation/AVFoundation.h>
@interface NotificationService ()
@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
@property (nonatomic, strong) AVSpeechSynthesizer *synth;
@property (nonatomic, strong) AVSpeechUtterance *utterance;
@end
@implementation NotificationService
- (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];
NSString *contentString = self.bestAttemptContent.body;
if (contentString != nil) {
[self speechWithString:contentString];
}
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);
}
// 語(yǔ)音播報(bào)
- (void)speechWithString:(NSString *)contentString {
self.utterance = [AVSpeechUtterance speechUtteranceWithString:contentString];
self.utterance.volume = 1.0f; //設(shè)置音量
self.utterance.rate = AVSpeechUtteranceDefaultSpeechRate; //設(shè)置語(yǔ)速
self.utterance.pitchMultiplier = 1; //設(shè)置語(yǔ)調(diào)
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
self.utterance.voice = voice;
self.synth = [[AVSpeechSynthesizer alloc] init];
[self.synth speakUtterance:self.utterance];
}
問題:缺陷就是只能在iOS10才有效须眷。
在做類似功能遇到的問題歡迎一起探討(⊙o⊙)