公司需求,實現(xiàn)類似支付寶一樣的后臺語音播報功能,之前用Notification Server Extension寫的.現(xiàn)在用Apple 的Voip重現(xiàn)實現(xiàn)了一下,目前是前臺的時候可以播報,縮進(jìn)后臺或者殺掉進(jìn)程之后只能收到通知,不能播放語音了,這個明天優(yōu)化,測試ok之后再發(fā)demo!

第一步:創(chuàng)建voip證書

第二部:打開Xcode后臺模式的voip權(quán)限

第三步:證書合成等待使用
制作.pem格式證書
1、將之前生成的voip.cer SSL證書雙擊導(dǎo)入鑰匙串
2、打開鑰匙串訪問,在證書中找到對應(yīng)voip.cer生成的證書,右鍵導(dǎo)出并選擇.p12格式,這里我們命名為voippush.p12,這里導(dǎo)出需要輸入密碼(隨意輸入,別忘記了)。
3、目前我們有兩個文件,voip.cer SSL證書和voippush.p12私鑰,新建文件夾命名為VoIP、并保存兩個文件到VoIP文件夾。
4、把.cer的SSL證書轉(zhuǎn)換為.pem文件,打開終端命令行cd到VoIP文件夾、執(zhí)行以下命令
openssl x509 -in voip.cer -inform der -out VoiPCert.pem
5、把.p12私鑰轉(zhuǎn)換成.pem文件,執(zhí)行以下命令(這里需要輸入之前導(dǎo)出設(shè)置的密碼)
openssl pkcs12 -nocerts -out VoIPKey.pem -in voippush.p12
6、再把生成的兩個.pem整合到一個.pem文件中
cat VoiPCert.pem VoIPKey.pem > ck.pem
最終生成的ck.pem文件一般就是服務(wù)器用來推送的。
第四步:寫代碼
注意點:
1.導(dǎo)入PushKit框架
2.遵守PKPushRegistryDelegate
3.發(fā)送的token是PKPushRegistry記錄的token,不再是推送的token,加粗表示重要
- (void)initPushkit{
? ? PKPushRegistry *pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
? ? pushRegistry.delegate=self;
? ? pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
? ? //ios10注冊本地通知
? ? if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
? ? ? ? [self registerUserNotificationCenter];
? ? }
}
- (void)registerUserNotificationCenter {
? ? if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
? ? ? ? //iOS10特有
? ? ? ? UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
? ? ? ? // 必須寫代理,不然無法監(jiān)聽通知的接收與點擊
? ? ? ? [centerrequestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
? ? ? ? ? ? if(granted) {
? ? ? ? ? ? ? ? // 點擊允許
? ? ? ? ? ? ? ? NSLog(@"注冊成功");
? ? ? ? ? ? ? ? [centergetNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
? ? ? ? ? ? ? ? ? ? NSLog(@"%@", settings);
? ? ? ? ? ? ? ? }];
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? // 點擊不允許
? ? ? ? ? ? ? ? NSLog(@"注冊失敗");
? ? ? ? ? ? }
? ? ? ? }];
? ? }
}
//應(yīng)用啟動此代理方法會返回設(shè)備Token 、一般在此將token上傳服務(wù)器
- (void)pushRegistry:(PKPushRegistry*)registry didUpdatePushCredentials:(PKPushCredentials*)credentials forType:(NSString*)type{
? ? NSLog(@"應(yīng)用啟動此代理方法會返回設(shè)備Token 、一般在此將token上傳服務(wù)器");
? ? NSString * token = [[[[credentials.token description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
//? ? self.clientId = token;//[token stringByReplacingOccurrencesOfString:@" " withString:@""];
? ? NSLog(@"token=%@",token);
}
//當(dāng)VoIP推送過來會調(diào)用此方法,一般在這里調(diào)起本地通知實現(xiàn)連續(xù)響鈴、接收視頻呼叫請求等操作
- (void)pushRegistry:(PKPushRegistry*)registry didReceiveIncomingPushWithPayload:(PKPushPayload*)payload forType:(NSString*)type {
? ? NSLog(@"當(dāng)VoIP推送過來會調(diào)用此方法,一般在這里調(diào)起本地通知實現(xiàn)連續(xù)響鈴、接收視頻呼叫請求等操作");
? ? [self showLocalNotification:payload.dictionaryPayload];
? ? [selfaddOperation:@"XXX收款100.99元"sound_type:@""];
}
/**
?當(dāng)APP收到呼叫、處于后臺時調(diào)用、用來處理通知欄類型和鈴聲。
?*/
- (void)showLocalNotification:(NSDictionary*)userInfo {
? ? if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
? ? ? ? UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
? ? ? ? UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
? ? ? ? content.body = [NSString localizedUserNotificationStringForKey:@"您的賬戶收到一筆巨款" arguments:nil];
? ? ? ? UNNotificationSound *customSound = [UNNotificationSound defaultSound];
? ? ? ? content.sound= customSound;
? ? ? ? UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? triggerWithTimeInterval:1repeats:NO];
? ? ? ? UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"voippush"
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? content:contenttrigger:trigger];
? ? ? ? [centeraddNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
? ? ? ? }];
? ? }else{
? ? ? ? UILocalNotification *callNotification = [[UILocalNotification alloc] init];
? ? ? ? callNotification.alertBody= [NSString
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? stringWithFormat:@"您的賬戶收到一筆巨款"];
? ? ? ? callNotification.soundName=@"default";
? ? ? ? [[UIApplication sharedApplication]
?? ? ? ? presentLocalNotificationNow:callNotification];
? ? }
}
直接粘貼到AppDelegate里面!
第五步:發(fā)送通知消息!
注意:PHP文件要和合成的pem證書放到一起,當(dāng)然你自己配置路徑也可以!

圈出來的是要修改的!
php文件鏈接:PHP文件
到此結(jié)束!!!
感謝博主:https://www.laileshuo.com/2019/05/13/ios-通過voip-push實現(xiàn)語音播報/
感謝網(wǎng)上認(rèn)識的小伙伴!
鄙視不安慰我失戀??的SB室友們!