首页 > 解决方案 > 应该在应用服务器上使用哪个证书来使用 Pushkit 和 APNS 唤醒 iOS 应用?

问题描述

我在我的 iOS 应用程序中使用 Websocket 进行数据传输。但是,由于有时当应用程序在后台挂起时,套接字会中断。在这种情况下,我使用 Voip 推送到 iOS 应用程序来唤醒应用程序。

//called on appDidFinishLaunching
//register for voip notifications

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

voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
voipRegistry.delegate = self;


//delegate methods for `PushKit`
#pragma mark - PushKit Delegate Methods

    - (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)pushCredentials forType:(PKPushType)type {
        self.myDeviceToken = [[[[pushCredentials token] description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSLog(@"voip token: %@", self.myDeviceToken);
    }

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

        if (![self socketIsConnected]) {
            [self reconnectSocket];
        }
    }

我将从我的登录 API 请求中收到的令牌didUpdatePushCredentials发送到我的应用服务器。

我心中有以下疑惑,并为之寻求答案。

  1. 是否PushKit需要 APNS 证书和 Voip 证书?或者只是其中一个,哪一个,为什么?
  2. 如果它需要这两个证书,我是否需要将这两个证书都保留在应用服务器上以向我的应用发送成功推送?
  3. 服务器端应该使用哪个证书来推送从服务器端调用“didReceiveIncomingPushWithPayload”的通知?

请在服务器端的代码下方找到:

private ApnsService getService(String appId) {

    synchronized (APP_ID_2_SERVICE_MAP) {

        ApnsService service = APP_ID_2_SERVICE_MAP.get(appId);

        if (service == null) {

            InputStream certificate = getCertificateInputStream(appId);

            if (certificate == null) {

                String errorMessage = "APNS appId unsupported: " + appId;

                LOGGER.error(errorMessage);

                throw new ATRuntimeException(errorMessage);

            }

            boolean useProd = useAPNSProductionDestination();

            if (useProd) {

                LOGGER.info("Using APNS production environment for app " + appId);

                service = APNS.newService().withCert(certificate, CERTIFICATE_PASSWORD).withProductionDestination()

                        .build();

            } else {

                LOGGER.info("Using APNS sandbox environment for app " + appId);

                service = APNS.newService().withCert(certificate, CERTIFICATE_PASSWORD).withSandboxDestination()

                        .build();

            }

            APP_ID_2_SERVICE_MAP.put(appId, service);

        }

        return service;

    }

}

我做了以下实施,但失败了: 1. 创建了 APNS SSL 服务证书沙盒 + 生产。2. 将在 didUpdatePushCredentials 中收到的令牌发送到服务器。3. 服务器使用 APNS 证书发送推送。但由于找不到任何相应的证书而失败。

因此,我无法将要发送到服务器的令牌与将在服务器上用于发送推送的证书相结合。

标签: iosapple-push-notificationsvoippushkitjavaapns

解决方案


看起来,你很困惑APNS并且PushKit

您需要先参考下图。

在此处输入图像描述.

它明确指出:

在通知服务器、 Apple Push Notification服务沙箱和生产环境之间建立连接,以将远程通知传送到您的应用程序。在使用 HTTP/2 时,相同的证书可用于传递应用程序通知、更新 ClockKit 复杂数据以及提醒后台 VoIP 应用程序传入 活动。您分发的每个应用程序都需要单独的证书。

这意味着单一证书适用于两者。

问题:1

PushKit 是否需要APNS证书和VOIP证书?或者只是其中一个,哪一个,为什么?

  • 通用证书对两者都有效。

问题2

如果它需要这两个证书,我是否需要将这两个证书都保留在应用服务器上以向我的应用发送成功推送?

  • 现在,这个问题没有意义。

更新:1

这似乎是与证书路径相关的问题。暂时保留,您可以使用以下php script用途触发通知

推送.php

<?php

// Put your device token here (without spaces):


$deviceToken = '1234567890123456789';
//


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

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



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

// Open a connection to the APNS server
$fp = stream_socket_client(
//  'ssl://gateway.push.apple.com:2195', $err,
'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(
'content-available'=> 1,
'alert' => $message,
'sound' => 'default',
'badge' => 0,
);



// 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);

您可以使用php push.php命令触发推送


推荐阅读