gpt4 book ai didi

ios - 实现 PushKit 并测试开发行为

转载 作者:IT王子 更新时间:2023-10-29 08:04:25 26 4
gpt4 key购买 nike

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

这是我的实现测试:

使用这 3 行代码,我可以在 didUpdatePushCredentials 回调上获取推送 token ,用于保存到我的服务器中。

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

服务器端我生成一个只有警报文本的“正常”有效负载推送通知,然后我发送到存储在我的服务器中的 voip token 。

我将回调与调试日志一起使用,但它们从未被调用过!
- (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 设备 token ,我永远不会收到 didReceiveIncomingPushWithPayload 回调的通知,但是从服务器我收到 200 ok 消息(消息已成功发送)

最佳答案

以防万一有人对使用 Pushkit 测试 voip 推送通知感兴趣,我留下了一个我成功遵循的小程序:

1 - 如果您还没有,请创建 企业社会责任 使用钥匙串(keychain)访问并在本地保存您的 CSR。

2 - 转到 Apple Developer 并获取访问证书、标识符和配置文件。在成员(member)中心。

  • 内部标识符-> 应用 ID 创建一个新的应用 ID
  • 内部设备-> 所有添加设备 UDID 您要用于测试 voip 推送
  • Inside Certificates-> All 创建一个新的生产证书:VoIP 服务证书。为您的 voip 服务证书选择先前创建的应用程序 ID。选择之前创建的 CSR(证书签名请求),创建后下载新的 voip_services.cer

  • 下载后双击 voip_services.cer 为了打开钥匙串(keychain)访问应用程序并导出生成证书的私钥:右键导出 证书.p12 文件。

    保存 voip_services.cer 证书.p12 文件夹中的文件以创建您的服务器推送通知生成器

    最后再次访问 Apple Developer 网站并在 Provisioning Profiles->Distribution create a new 中Ad-Hoc 分布配置文件 包括您要用于测试 voip 推送的所有设备 UDID。下载此配置文件并将其拖放到您的 xcode 中,以便在您的应用程序中使用它。

    现在让我们创建将接收 voip 推送通知的 iOS 应用程序:
  • 从 Xcode 新项目菜单创建一个新的单一 View 应用程序。
  • 根据上一节中创建的应用程序 id 填写其 bundle Identifier。
  • 在 General-> Linked Frameworks and Libraries 中添加 PushKit.framework。
  • 在功能中启用后台模式并选择 IP 语音选项。
  • 在 Build Settings -> Code Signing 中选择您之前下载的配置文件,然后选择 Distribution 作为 Code Signing Identity。

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

    在您的根 View Controller header 中 ( ViewController.h
    ) PushKit.framework 的导入:
    #import <PushKit/PushKit.h>

    添加委托(delegate)以实现其功能:
    @interface ViewController : UIViewController <PKPushRegistryDelegate>

    添加根 View Controller (ViewController.m)推送注册的 viewDidLoad 函数:
    - (void)viewDidLoad {
    [super viewDidLoad];

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

    实现所需的委托(delegate)功能:
    - (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
  • 证书.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

    为了发送推送,您可以使用 Pusher来自 raywenderlich tutorials教程或使用简单的 php 脚本:
    <?php

    // Put your device token here (without spaces):
    $deviceToken = '0f744707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bbad78';

    // Put your private key's passphrase here:
    $passphrase = 'pushchat';

    // Put your alert message here:
    $message = 'My first push notification!';

    ////////////////////////////////////////////////////////////////////////////////

    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

    // Open a connection to the APNS server
    $fp = stream_socket_client(
    'ssl://gateway.sandbox.push.apple.com:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

    if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);

    echo 'Connected to APNS' . PHP_EOL;

    // Create the payload body
    $body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
    );

    // Encode the payload as JSON
    $payload = json_encode($body);

    // Build the binary notification
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

    // Send it to the server
    $result = fwrite($fp, $msg, strlen($msg));

    if (!$result)
    echo 'Message not delivered' . PHP_EOL;
    else
    echo 'Message successfully delivered' . PHP_EOL;

    // Close the connection to the server
    fclose($fp);

    你应该在脚本中修改:
  • $deviceToken 通过添加您的 PushCredentials(来自应用程序日志)
  • $passphrase 由您在第 2 步创建 PushVoipKey.pem 时添加的密码短语

  • 就是这样。执行php脚本:
    #php simplePushScript.php

    并且您应该会收到您的 voip 推送通知(您应该会看到应用日志:“didReceiveIncomingPushWithPayload”)

    在那次测试之后,我想知道如何通过 pushkit 框架接收标准推送通知,但不幸的是我没有答案,因为在注册推送类型时我找不到任何其他 PKPushType 但 PKPushTypeVoIP ...
    pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];

    就这样!谢谢阅读!

    关于ios - 实现 PushKit 并测试开发行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27245808/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com