??由于iOS 10禁止了VoIP類應(yīng)用常駐后臺的權(quán)限,導(dǎo)致Xcode 8 打包出來的VoIP類應(yīng)用后臺長連接失效;官方推薦的方法是PushKit + CallKit,今天先來介紹下PushKit的使用。
官方文檔:PKPushRegistry
證書配置就不在這里說了,下面開始劃重點(diǎn),也是我最想知道的幾點(diǎn):
1.該證書可與普通推送證書同時(shí)存在,且均可使用,但使用時(shí)注意這兩種生成的token值是不同的
2.PushKit推送可在程序被殺死情況下執(zhí)行代碼,由于自定義程度比較高,目前貌似沒有第三方推送支持
PushKit.jpeg
下面看一下實(shí)現(xiàn),在Build Phases中加入PushKit.framework,AppDelegate中添加頭文件#import <PushKit/PushKit.h>,添加代理PKPushRegistryDelegate
注冊通知
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
PKPushRegistry *pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
pushRegistry.delegate = self;
pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
UIUserNotificationSettings *userNotifySetting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:userNotifySetting];
return YES;
}
PushKit代理
//獲取token
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type{
NSString *str = [NSString stringWithFormat:@"%@",credentials.token];
NSLog(@"tokenStr = %@",str);
}
//收到推送后所走的方法,注意PushKit不會自己彈出推送框,如果需要自己寫本地推送即可
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type {
//在這里就可以做一些操作,比如使用CallKit喚起系統(tǒng)通話界面
UILocalNotification *backgroudMsg = [[UILocalNotification alloc] init];
if (backgroudMsg) {
backgroudMsg.timeZone = [NSTimeZone defaultTimeZone];
backgroudMsg.alertBody = @"VoIP來電";
backgroudMsg.alertAction = @"查看";
NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];;
backgroudMsg.userInfo = infoDic;
[[UIApplication sharedApplication] presentLocalNotificationNow:backgroudMsg];
//[self cerateAVAudioPlayer];
}
}
原生推送代理
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSString *token = [NSString stringWithFormat:@"%@", deviceToken];
}
?
??找了好幾個(gè)PHP文件都運(yùn)行不起來,后來在網(wǎng)上發(fā)現(xiàn)了PushMeBaby
,先用這個(gè)進(jìn)行測試吧,等有空再去找服務(wù)器代碼。
下載好了之后,打開工程將voip_services.cer加入到工程里,修改token值跟證書路徑,運(yùn)行后會報(bào)錯(cuò),將那一行直接改為#import <MacTypes.h>,再運(yùn)行即可(沒反應(yīng)多運(yùn)行幾遍)。
- (id)init {
self = [super init];
if(self != nil) {
//注意token要帶空格
self.deviceToken = @"26f7b0a8 efa8e6b3 6dde46c5 8d1a2cf8 e3583e31 87feab7d af6c8a54 24896f55";
self.payload = @"{\"aps\":{\"alert\":\"This is some fancy message.\",\"badge\":1}}";
self.certificate = [[NSBundle mainBundle] pathForResource:@"aps_development" ofType:@"cer"];
}
return self;
}