PushKit 佔坑

最近項目要作關於voip業務,咱們都知道蘋果後臺是一個假後臺,當程序退出到後臺時,socket是會斷開鏈接,程序是被掛起的。咱們要作的就是相似QQ 微信那種,在程序退到後臺時,有電話來時彈出一個通知。要了解pushkit概述請參考下面鏈接php

百度某大神的博客http://blog.csdn.net/openglnewbee/article/details/44807191java

  • 1.證書建立
    首先建立voip證書

    0AF8B321-63B9-40CD-88D0-8D782603CB5E.png


    67DAF714-175D-4BB6-A390-258869E22ACF.png

    一步一步往下建立,最後生成下載證書雙擊安裝到鑰匙串。
    當安裝到鑰匙串完成後, 注意:咱們還須要另外建立一個配置文件

7D841F9B-4B15-4809-A7A6-D9149C075538.png


![Uploading CBD67474-28EC-412D-94DD-7F2DD75E1071_112078.png . . .]

建立完成後下載 雙擊安裝就好了。ios

  • 2.接下來上代碼
    1. 須要導入push kit框架#import <PushKit/PushKit.h>
    2. 註冊通知與pushkit,pushkit要ios8 及之後纔可使用
if (CurrentSystemVersion.floatValue >= 8.0) { UIUserNotificationSettings *userNotifiSetting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:userNotifiSetting]; PKPushRegistry *pushRegistry = [[PKPushRegistry alloc] initWithQueue:nil]; pushRegistry.delegate = self; pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP]; }

3.實現代理方法1json

- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type{ NSString *str = [NSString stringWithFormat:@"%@",credentials.token]; _tokenStr = [[[str stringByReplacingOccurrencesOfString:@"<" withString:@""]  stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""]; } //這個代理方法是獲取了設備的惟tokenStr,是要給服務器的

與apns推送不一樣,pushjit的token獲取跟apnstoken的獲取方法不一樣,apps在服務器

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{ [application registerForRemoteNotifications];//必須先實現這個方法,纔會走下面的方法 } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ NSLog(@"%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""] stringByReplacingOccurrencesOfString: @">" withString: @""] stringByReplacingOccurrencesOfString: @" " withString: @""]); NSString *token = [NSString stringWithFormat:@"%@", deviceToken]; //獲取終端設備標識,這個標識須要經過接口發送到服務器端,服務器端推送消息到APNS時須要知道終端的標識,APNS經過註冊的終端標識找到終端設備 NSLog(@"%@",token); }

獲取設備的token,這兩個token的值是不一樣的,注意不要搞混了。微信

實現代理方法2app

- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pancal) name:@"precancel" object:nil]; NSDictionary *dic = [self jsonToDictionary:[[payload.dictionaryPayload objectForKey:@"aps"] objectForKey:@"alert"]]; if ([[dic objectForKey:@"cmd"] isEqualToString:@"precall"]) { UIUserNotificationType theType = [UIApplication sharedApplication].currentUserNotificationSettings.types; if (theType == UIUserNotificationTypeNone) { UIUserNotificationSettings *userNotifySetting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:userNotifySetting]; } UILocalNotification *backgroudMsg = [[UILocalNotification alloc] init]; if (backgroudMsg) { backgroudMsg.timeZone = [NSTimeZone defaultTimeZone]; backgroudMsg.alertBody = @"門口機來電"; backgroudMsg.alertAction = @"查看"; //設置通知的相關信息,這個很重要,能夠添加一些標記性內容,方便之後區分和獲取通知的信息 NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];; backgroudMsg.userInfo = infoDic; [[UIApplication sharedApplication] presentLocalNotificationNow:backgroudMsg]; [self cerateAVAudioPlayer]; } }else if ([[dic objectForKey:@"cmd"] isEqualToString:@"precancel"]){ [[NSNotificationCenter defaultCenter] postNotificationName:@"precancel" object:nil]; [self pancalStopSound]; }

若是一切正常,就算程序殺掉進程,重啓,退到後臺,服務器推送過來的消息都會走代理方法2,在這裏咱們能夠作一些處理,我這裏是彈出了一個本地通知,而且播放提示音效。框架

使用push kit的優勢socket

1.應用的voip長鏈接不保持,在收到呼叫或者發起呼叫時再鏈接;
2.當呼叫發送到voip 服務器時,對端若不在線,經過voip 服務器鏈接到pushserver向對端發push通知;
3.應用收到voip push通知時,迅速完成註冊;
4.呼叫方經過延時操做等邏輯(複雜一點對voip服務器進行改造,被叫鏈接上來之後通知到主叫側),再次發起呼叫,通話即成功創建。post

java後臺服務器搭建

public static void main(String[] args) throws Exception { try { //從客戶端獲取的deviceToken,在此爲了測試簡單,寫固定的一個測試設備標識。 String deviceToken = "df779eda 73258894 5882ec78 3ac7b254 6ebc66fe fa295924 440d34ad 6505f8c4" System.out.println("Push Start deviceToken:" + deviceToken); //定義消息模式 PayLoad payLoad = new PayLoad(); payLoad.addAlert("this is test!"); payLoad.addBadge(1);//消息推送標記數,小紅圈中顯示的數字。 payLoad.addSound("default"); //註冊deviceToken PushNotificationManager pushManager = PushNotificationManager.getInstance(); pushManager.addDevice("iPhone", deviceToken); //鏈接APNS String host = "gateway.sandbox.push.apple.com"; //String host = "gateway.push.apple.com"; int port = 2195; String certificatePath = "c:/PushTest.p12";//前面生成的用於JAVA後臺鏈接APNS服務的*.p12文件位置 String certificatePassword = "123456";//p12文件密碼。 pushManager.initializeConnection(host, port, certificatePath, certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12); //發送推送 Device client = pushManager.getDevice("iPhone"); System.out.println("推送消息: " + client.getToken()+"\n"+payLoad.toString() +" "); pushManager.sendNotification(client, payLoad); //中止鏈接APNS pushManager.stopConnection(); //刪除deviceToken pushManager.removeDevice("iPhone"); System.out.println("Push End"); } catch (Exception ex) { ex.printStackTrace(); } } }

注意: 用java搭建的後臺服務器咱們須要提供給服務器.p12文件,用php搭建的服務器咱們須要給服務器提供.pem文件

.p12文件導出


DA909015-E4C2-479D-A27B-46E700428C7A.png


右鍵導出文件便可。

.pem文件導出稍微複雜

參考 簡書做者《iOS原生APNS推送之PHP後臺的pem證書製做流程》

pushkit使用就到這裏結束了,是否是很簡單呢,趕忙來一塊兒愉快玩耍吧。附上使用截圖

相關文章
相關標籤/搜索