最近項(xiàng)目要做關(guān)于voip業(yè)務(wù),我們都知道蘋果后臺是一個(gè)假后臺,當(dāng)程序退出到后臺時(shí),socket是會斷開連接,程序是被掛起的。我們要做的就是類似QQ 微信那種,在程序退到后臺時(shí),有電話來時(shí)彈出一個(gè)通知。要了解pushkit概述請參考下面連接
百度某大神的博客http://blog.csdn.net/openglnewbee/article/details/44807191
- 1.證書創(chuàng)建
首先創(chuàng)建voip證書
0AF8B321-63B9-40CD-88D0-8D782603CB5E.png
67DAF714-175D-4BB6-A390-258869E22ACF.png
一步一步往下創(chuàng)建,最后生成下載證書雙擊安裝到鑰匙串。
當(dāng)安裝到鑰匙串完成后, 注意:我們還需要另外創(chuàng)建一個(gè)配置文件


創(chuàng)建完成后下載 雙擊安裝就行了。
- 2.接下來上代碼
- 需要導(dǎo)入push kit框架
#import <PushKit/PushKit.h> - 注冊通知與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.實(shí)現(xiàn)代理方法1
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type{
NSString *str = [NSString stringWithFormat:@"%@",credentials.token];
_tokenStr = [[[str stringByReplacingOccurrencesOfString:@"<" withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
} //這個(gè)代理方法是獲取了設(shè)備的唯tokenStr,是要給服務(wù)器的
與apns推送不同,pushjit的token獲取跟apnstoken的獲取方法不同,apps在
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
[application registerForRemoteNotifications];//必須先實(shí)現(xiàn)這個(gè)方法,才會走下面的方法
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSLog(@"%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""] stringByReplacingOccurrencesOfString: @">" withString: @""] stringByReplacingOccurrencesOfString: @" " withString: @""]);
NSString *token = [NSString stringWithFormat:@"%@", deviceToken];
//獲取終端設(shè)備標(biāo)識,這個(gè)標(biāo)識需要通過接口發(fā)送到服務(wù)器端,服務(wù)器端推送消息到APNS時(shí)需要知道終端的標(biāo)識,APNS通過注冊的終端標(biāo)識找到終端設(shè)備
NSLog(@"%@",token);
}
獲取設(shè)備的token,這兩個(gè)token的值是不同的,注意不要搞混了。
實(shí)現(xiàn)代理方法2
- (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 = @"門口機(jī)來電";
backgroudMsg.alertAction = @"查看";
//設(shè)置通知的相關(guān)信息,這個(gè)很重要,可以添加一些標(biāo)記性內(nèi)容,方便以后區(qū)分和獲取通知的信息
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];
}
如果一切正常,就算程序殺掉進(jìn)程,重啟,退到后臺,服務(wù)器推送過來的消息都會走代理方法2,在這里我們可以做一些處理,我這里是彈出了一個(gè)本地通知,并且播放提示音效。
使用push kit的優(yōu)點(diǎn)
1.應(yīng)用的voip長連接不保持,在收到呼叫或者發(fā)起呼叫時(shí)再連接;
2.當(dāng)呼叫發(fā)送到voip 服務(wù)器時(shí),對端若不在線,通過voip 服務(wù)器連接到pushserver向?qū)Χ税l(fā)push通知;
3.應(yīng)用收到voip push通知時(shí),迅速完成注冊;
4.呼叫方通過延時(shí)操作等邏輯(復(fù)雜一點(diǎn)對voip服務(wù)器進(jìn)行改造,被叫連接上來以后通知到主叫側(cè)),再次發(fā)起呼叫,通話即成功建立。
java后臺服務(wù)器搭建
public static void main(String[] args) throws Exception
{
try
{
//從客戶端獲取的deviceToken,在此為了測試簡單,寫固定的一個(gè)測試設(shè)備標(biāo)識。
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);//消息推送標(biāo)記數(shù),小紅圈中顯示的數(shù)字。
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服務(wù)的*.p12文件位置
String certificatePassword = "123456";//p12文件密碼。
pushManager.initializeConnection(host, port, certificatePath, certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
//發(fā)送推送
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搭建的后臺服務(wù)器我們需要提供給服務(wù)器.p12文件,用php搭建的服務(wù)器我們需要給服務(wù)器提供.pem文件
.p12文件導(dǎo)出

右鍵導(dǎo)出文件即可。
.pem文件導(dǎo)出稍微復(fù)雜
pushkit使用就到這里結(jié)束了,是不是很簡單呢,趕緊來一起愉快玩耍吧。附上使用截圖

