应该在应用服务器上使用哪个证书来使用 Pushkit 和 APNS 唤醒 iOS 应用? [英] Which certificate should be used on app Server for waking iOS app using Pushkit and APNS?

查看:20
本文介绍了应该在应用服务器上使用哪个证书来使用 Pushkit 和 APNS 唤醒 iOS 应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

//在appDidFinishLaunching上调用//注册voip通知PKPushRegistry *voipRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];voipRegistry.delegate = self;//委托`PushKit`的方法#pragma mark - PushKit 委托方法- (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]) {[自重连接套接字];}}

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

我心中有以下疑问,正在寻找答案.

<块引用>

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

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

private ApnsService getService(String appId) {同步(APP_ID_2_SERVICE_MAP){ApnsService 服务 = APP_ID_2_SERVICE_MAP.get(appId);如果(服务 == 空){InputStream 证书 = getCertificateInputStream(appId);如果(证书 == 空){String errorMessage = "APNS appId 不受支持:" + appId;LOGGER.error(errorMessage);抛出新的 ATRuntimeException(errorMessage);}boolean useProd = useAPNSProductionDestination();如果(使用产品){LOGGER.info("使用APNS生产环境为app" + appId);service = APNS.newService().withCert(certificate, CERTIFICATE_PASSWORD).withProductionDestination().建造();} 别的 {LOGGER.info("使用APNS沙箱环境为应用程序" + appId);service = APNS.newService().withCert(certificate, CERTIFICATE_PASSWORD).withSandboxDestination().建造();}APP_ID_2_SERVICE_MAP.put(appId, service);}退货服务;}}

我执行了以下操作,但失败了:1、创建APNS SSL服务证书沙箱+生产.2. 将在 didUpdatePushCredentials 中收到的令牌发送到服务器.3.服务器使用APNS证书发送推送.但由于找不到相应的证书而失败.

因此,我无法将要发送到服务器的令牌和将在服务器上用于发送推送的证书结合起来.

解决方案

看来,你对 APNSPushKit

感到困惑

您需要先参考下图.

.

它明确指出:

<块引用>

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

这意味着单个证书对两者都有效.

问题:1

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

  • 通用证书适用于两者.

问题:2

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

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

更新:1

这似乎是与证书路径相关的问题.就这样吧,你可以使用下面的php script来触发通知

推送.php

$消息,'声音' =>'默认','徽章' =>0,);//将有效负载编码为 JSON$payload = json_encode($body);//构建二进制通知$msg = chr(0) .包('n', 32) .pack('H*', $deviceToken) .pack('n', strlen($payload)) .$有效载荷;//发送到服务器$result = fwrite($fp, $msg, strlen($msg));如果(!$结果)echo '消息未送达'.PHP_EOL;别的echo '消息发送成功'.PHP_EOL;//关闭与服务器的连接fclose($fp);

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

I am using Websocket in my iOS app for data transfer. But, since sometimes when the app is suspended in the background, the socket breaks. In that case, I use Voip push to iOS app to wake app up.

//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];
        }
    }

I send the token received from didUpdatePushCredentials in my Login API request to my app server.

I have the following doubts in my mind and seeking answers for them.

  1. Does PushKit require both the APNS certificate and Voip certificate? or just one of them and which one and why?
  2. If it requires both the certificates, do I need to keep both the certificates on the app server for sending success pushes to my app?
  3. Which certificate should be used on the server side to push notification which would invoke "didReceiveIncomingPushWithPayload" from server side?

Please find below the code on server side :

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;

    }

}

I did following implementation and it failed : 1. Created APNS SSL Service Certificate Sandbox + Production. 2. Sent token received in didUpdatePushCredentials to server. 3. Server used APNS Certificate to send the push. But failed since it could not find any corresponding certificate.

So I am failing at the combination of token to be sent to server and the certificate that would be used on the server to send the push.

解决方案

It seems like, You're confused about APNS and PushKit

You need to refer the below image first.

.

It clearly states:

Establish connectivity between your notification server, the Apple Push Notification service sandbox, and production environments to deliver remote notifications to your app. When utilizing HTTP/2, the same certificate can be used to deliver app notifications, update ClockKit complication data, and alert background VoIP apps of incoming activity. A separate certificate is required for each app you distribute.

That's means single certificate worked for both.

Question: 1

Does PushKit require both the APNS certificate and VOIP certificate? or just one of them and which one and why?

  • The common certificate worked for both.

Question: 2

If it requires both the certificates, do I need to keep both the certificates on the app server for sending success pushes to my app?

  • Now, No meaning of this question.

Update: 1

It seems like an issue related to the certificate path. Leave it as of now, You can use below php script use to trigger notification

Push.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);

You can trigger push by using php push.php command

这篇关于应该在应用服务器上使用哪个证书来使用 Pushkit 和 APNS 唤醒 iOS 应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆