上個版本的開發計劃中產品同窗建議在咱們的商家版App中作後臺語音播報功能,在評審的時候我就在想,徹底能夠經過Push靜默推送來實現後臺播放音頻來實現(後續事實證實,這是個大坑)。bash
關於靜默推送 推薦你們看一下https://www.jianshu.com/p/c211bd295d58session
好了,最後經過push和backGroundModes實現了在後臺語言播放的功能,工程的配置: app
在iOS 10 UNNotificationServiceExtension 剛出現的時候瞭解過,而後在後續沒有用到就忘的差很少了,在對它進一步瞭解以後,我發現它徹底可以在不開啓後臺運行相關功能的狀況下來實現語音播報功能,好吧,開始動手:測試
在你的功能首先建立UNNotificationServiceExtension targetui
建立好了把相關音頻文件導入:spa
- (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];
[[AVAudioSession sharedInstance] setActive:YES error:NULL];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
NSDictionary *userInfo = self.bestAttemptContent.userInfo;
NSDictionary * aps = [userInfo objectForKey:@"aps"];
NSString * soundCommand = [aps valueForKey:@"soundCommand"];
[self playSoundsWithSoundCommand:soundCommand];
self.contentHandler(self.bestAttemptContent);
}
/*播放*/
-(void)playWithFileUrlString:(NSString *)fileURLString{
if (![fileURLString length]) {
return;
}
AVAudioSession * session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
BOOL ret = [session setCategory:AVAudioSessionCategoryPlayback error:nil];
NSLog(@"%d",ret);
NSURL *fileURL = [[NSBundle mainBundle]URLForResource:fileURLString withExtension:@".mp3"];
static SystemSoundID soundID = 0;
AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(fileURL), &soundID);
AudioServicesPlayAlertSoundWithCompletion(soundID, ^{
NSLog(@"播放完成");
});
}
複製代碼
記住了在作完相關操做以後再調用self.contentHandler(self.bestAttemptContent);方法 進入墓碑模式(不執行應用程序的任何代碼)3d
還有很重要的一點,記住push的試試讓後臺同窗要加入一個參數"mutable-content" = 1;,否則咱們的擴展類方法是攔截不到推送的哦,要和alert 同級的,位置不要錯。code
接下來運行-測試,完美實現。打包,而後又報錯了,看了緣由是由於擴展target和個人原來工程的簽名不是同一team,這時候就要用的appid建立的時候建立一個通配符appid了。orm
在你開發者中心建立一個通配符appid包含到你的擴展應用下,而後生成相關開發和生成Profile文件,下載下來,而後打包。成功!!!cdn
接下來就是等待蘋果爸爸的審覈了,不過十拿九穩啦~