美文网首页
PushKit实践记录

PushKit实践记录

作者: runsuc | 来源:发表于2023-04-20 14:42 被阅读0次

    前提:使用的iOS16系统测试

    1.需要引入PushKit和CallKit,两个都需要

    #import <PushKit/PushKit.h>

    #import <CallKit/CallKit.h>

    如果没有用CallKit,会存在推送成功,但是手机上app不在前台收不到回调的情况(后台或者杀死app情况,不会唤起app)

    2.注册pushkit

        PKPushRegistry *pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];

        pushRegistry.delegate=self;

        pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];

    3.上报token

    #pragma mark PKPushRegistryDelegate

    - (void)pushRegistry:(PKPushRegistry*)registrydidUpdatePushCredentials:(PKPushCredentials*)credentialsforType:(NSString*)type

    {

        if ([type isEqualToString:PKPushTypeVoIP])

        {

            constunsigned*tokenBytes = (constunsigned*)[credentials.tokenbytes];

            NSString *result = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",

                                                ntohl(tokenBytes[0]),ntohl(tokenBytes[1]),

                                                ntohl(tokenBytes[2]),ntohl(tokenBytes[3]),

                                                ntohl(tokenBytes[4]),ntohl(tokenBytes[5]),

                                                ntohl(tokenBytes[6]),ntohl(tokenBytes[7])];

            NSLog(@"pk token str = [%@]",result);

        }

    }

    4.收到pushkit推送触发的回调

    - (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type withCompletionHandler:(nonnull void (^)(void))completion

    {

        NSLog(@"receive payload %@ type %@", payload.dictionaryPayload,type);

        NSNumber*badge = payload.dictionaryPayload[@"aps"][@"badge"];

        badge =@99;

        if([badgeisKindOfClass:[NSNumberclass]])

        {

            [UIApplication sharedApplication].applicationIconBadgeNumber = [badge integerValue];

        }

        CXHandle *callHandle = [[CXHandle alloc] initWithType:CXHandleTypePhoneNumber value:@"123"];

        CXCallUpdate*callUpdate = [[CXCallUpdatealloc]init];

        callUpdate.remoteHandle= callHandle;

        callUpdate.localizedCallerName=@"John Smith";

        callUpdate.supportsHolding=NO;

        callUpdate.supportsGrouping=NO;

        callUpdate.supportsUngrouping=NO;

        callUpdate.hasVideo=NO;

        CXProviderConfiguration *config = [[CXProviderConfiguration alloc] initWithLocalizedName:@"My App"];

        config.maximumCallsPerCallGroup = 1;

        config.supportsVideo=NO;

        CXProvider*provider = [[CXProvideralloc]initWithConfiguration:config];

        [providerreportNewIncomingCallWithUUID:[NSUUID UUID] update:callUpdate completion:^(NSError * _Nullable error) {

            completion();

        }];

    }

    这个里面要处理使用CallKit来reportNewIncomingCallWithUUID。不然在后台虽然走了这个回调,会crash,Killing app because it never posted an incoming call to the system after receiving a PushKit VoIP push。表示您的应用程序在接收到VoIP推送后未能将来电事件通知系统导致crash。

    5.要添加xcode项目配置

    相关文章

      网友评论

          本文标题:PushKit实践记录

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