ios – 实现PushKit并在开发行为中进行测试

我想在我的应用程序(Voip应用程序)中实现PushKit服务,但我有以下疑问:我看到我可以生成只有生产voip证书,如果我尝试在开发设备上测试voip推送通知服务,它是有效的?

这是我的实现测试:

使用这3行代码,我可以在我用来保存到我的服务器的didUpdatePushCredentials回调上获取推送令牌.

PKPushRegistry *pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
pushRegistry.delegate = self;
pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];

服务器端我生成一个只有警报文本的“正常”有效载荷推送通知,我发送到存储在我的服务器中的voip令牌.

我使用回调与调试日志,但他们从来没有被调用!

- (void)pushRegistry:(PKPushRegistry *)registry didInvalidatePushTokenForType:(NSString *)type {

          NSLog(@"didInvalidatePushTokenForType");

}

-(void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type {

          NSLog(@"didReceiveIncomingPushWithPayload: %@",payload.description);

}

-(void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type {
     if([credentials.token length] == 0) {
           NSLog(@"voip token NULL");

          return;
     }

      NSLog(@"didUpdatePushCredentials: %@ - Type: %@",credentials.token,type);

}

如果我尝试从我的服务器生成推送通知消息到以前上传的voip设备令牌,我从来没有通知didReceiveIncomingPushWithPayload回调,但是从服务器我得到200 ok消息(消息已成功发送)

解决方法

为了防止有人有兴趣通过Pushkit测试voip push通知,我留下了一个小程序,我顺利地遵循:

1 – 创建(如果您还没有)具有钥匙串访问权限的CSR,并在本地保存您的CSR.

2 – 转到Apple Developer,获取证书,标识符和配置文件.在会员中心

>内部标识符 – >应用ID创建一个新的应用ID
>内部设备 – >全部添加要用于测试voip推送的设备UDID
>内部证书 – >全部创建新的生产证书:VoIP服务证书.为您的voip服务证书选择先前创建的应用程序编号.选择先前创建的CSR(证书签名请求),一旦创建下载您的新的voip_services.cer

一旦下载,双击voip_services.cer以打开钥匙串访问应用程序并导出生成证书的私钥:右键导出certificate.p12文件.

将voip_services.cer和certificate.p12文件保存在文件夹中,以创建服务器推送通知生成器

最后再次到配置配置文件 – > Distribution中的Apple Developer网站,创建一个新的Ad-Hoc分发配置文件,其中包含您要用于测试voip推送的所有设备UDID.下载此配置文件并将其拖放到您的xcode中,以便在您的应用程序中使用它.

现在可以创建将接收voip push通知的iOS应用程序:

>从Xcode新项目菜单创建一个新的单一视图应用程序.
>根据上一节中创建的应用程序ID填写其标识符.
>在General中添加PushKit.framework→>链接框架和库.
>功能启用后台模式并选择基于IP的语音选项.
>在构建设置 – >代码签名选择您以前下载的配置配置文件,并选择分发为代码签名身份.

让我们在应用程序中添加代码Pasquale在他的问题中添加:

在您的根视图控制器头(ViewController.h
)PushKit.framework的导入:

#import <PushKit/PushKit.h>

添加代表以实现其功能:

@interface ViewController : UIViewController <PKPushRegistryDelegate>

添加您的根视图控件(ViewController.m)的viewDidLoad函数push注册:

- (void)viewDidLoad {
    [super viewDidLoad];

    PKPushRegistry *pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
    pushRegistry.delegate = self;
    pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
}

实施所需的委托功能:

- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type{
    if([credentials.token length] == 0) {
        NSLog(@"voip token NULL");
        return;
    }

    NSLog(@"PushCredentials: %@",credentials.token);
}

- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type
{
    NSLog(@"didReceiveIncomingPushWithPayload");
}

一旦所有的编译和确定,存档您的项目并导出您的ipa文件,以便安装在测试设备上(您可以使用例如Testflight来完成这项工作).

执行它并从日志中获取我们将用于发送推送的PushCredentials.

现在让我们去服务器端(我按照raywenderlich tutorials的这个很好的指导):

回到文件夹你放置了三个文件:

> voip_services.cer
> certificate.p12

1 – 打开终端并从证书文件创建pem文件:

#openssl x509 -in voip_services.cer -inform der -out PushVoipCert.pem

2 – 从导出的私钥文件创建pem文件:

#openssl pkcs12 -nocerts -out PushVoipKey.pem -in certificate.p12

3 – 将两个pem文件一起加入:

#cat PushVoipCert.pem PushVoipKey.pem > ck.pem

raywenderlich tutorials获取Simplepush脚本教程并进行修改

> $deviceToken通过添加您的PushCredentials(从应用程序日志)
> $passphrase通过您在创建PushVoipKey.pem步骤2时添加的密码

而已.执行php脚本:

#php simplepush.php

你应该收到你的voip push通知(你应该看到应用程序日志:“didReceiveIncomingPushWithPayload”)

在测试之后,我想知道如何通过pushkit框架接收标准推送通知,但不幸的是我没有回答,因为注册推式时找不到任何其他PKPushType,但是PKPushTypeVoIP …

pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];

就这样!谢谢阅读!

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...