美文网首页iOS开发技巧
iOS PushKit的使用

iOS PushKit的使用

作者: 天明依旧 | 来源:发表于2017-02-13 16:34 被阅读380次

      由于iOS 10禁止了VoIP类应用常驻后台的权限,导致Xcode 8 打包出来的VoIP类应用后台长连接失效;官方推荐的方法是PushKit + CallKit,今天先来介绍下PushKit的使用。
    官方文档:PKPushRegistry

    证书配置就不在这里说了,下面开始划重点,也是我最想知道的几点:

    1.该证书可与普通推送证书同时存在,且均可使用,但使用时注意这两种生成的token值是不同的
    2.PushKit推送可在程序被杀死情况下执行代码,由于自定义程度比较高,目前貌似没有第三方推送支持

    PushKit.jpeg

    下面看一下实现,在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唤起系统通话界面
        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];
    
    }
    
    


      找了好几个PHP文件都运行不起来,后来在网上发现了PushMeBaby
    ,先用这个进行测试吧,等有空再去找服务器代码。

    下载好了之后,打开工程将voip_services.cer加入到工程里,修改token值跟证书路径,运行后会报错,将那一行直接改为#import <MacTypes.h>,再运行即可(没反应多运行几遍)。

    - (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;
    }
    
    

    相关文章

      网友评论

      • SoulmateA:测试成功了,但是好像不是pushkit 走的是apns啊
        SoulmateA:@天明依旧 已经成功了 谢谢大哥
        天明依旧:看看是不是token用成了apns的了
      • SoulmateA:大哥,怎么老是报 SSLWrite(): -9806 0 这个错误呢

      本文标题:iOS PushKit的使用

      本文链接:https://www.haomeiwen.com/subject/ymxtwttx.html