PushKit实践记录

前提:使用的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项目配置

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容