接入極光推送實(shí)現(xiàn)前臺(tái)語(yǔ)音播報(bào)后臺(tái)鎖屏自定義消息并適配版本
1.首先實(shí)現(xiàn)語(yǔ)音播報(bào),請(qǐng)看我的iOS 文字轉(zhuǎn)語(yǔ)音的三種方案
2.如何實(shí)現(xiàn)自定義聲音
對(duì)應(yīng)音頻文件格式是 aiff,wav,caf 文件,文件也必須放到 app 的 mainBundle 目錄中。
自定義通知聲音的播放時(shí)間必須在 30s 內(nèi),如果超過(guò)這個(gè)限制,則將用系統(tǒng)默認(rèn)通知聲音替代。
可以使用 afconvert 工具來(lái)處理音頻文件格式,在終端中敲入如下命令就可以將一個(gè) mp3 文件轉(zhuǎn)換成 caf 文件:
$ afconvert sound.mp3 sound.caf -d ima4 -f caff -v
自己在極光測(cè)試,在上圖填寫。
3.主要實(shí)現(xiàn)著3個(gè)回調(diào)函數(shù)
<pre>
iOS 10會(huì)調(diào)用下面的的函數(shù)3.2/3.3 其他調(diào)用3.1
在iOS 10默認(rèn)不提示彈框、聲音所以不需要做處理,3.2需要做處理處理 completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert); // 需要執(zhí)行這個(gè)方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以設(shè)置 (內(nèi)參數(shù)任意刪除,這里我只去除了聲音)
3.1///基于iOS 7 及以上的系統(tǒng)版本
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^) (UIBackgroundFetchResult))completionHandler
3.2
ifdef NSFoundationVersionNumber_iOS_9_x_Max
pragma mark- JPUSHRegisterDelegate
-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler
3.3
-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
</pre>
4.實(shí)現(xiàn)之后還會(huì)出現(xiàn)一個(gè)問(wèn)題重復(fù)播報(bào),需要記錄一下內(nèi)容,重復(fù)返回,以下是我簡(jiǎn)單實(shí)現(xiàn)的方法
<pre>
if (self.recordDict.allKeys.count > 10) {
[self.recordDict removeAllObjects];
}
if (self.recordDict[string] != nil && [self.recordDict[string] isEqualToString:string]) {
return;
} else {
[self.recordDict setValue:string forKey:string];
}
</pre>