我正在我的iOS应用中使用Websocket进行数据传输。但是,由于有时当应用程序在后台挂起时,套接字会中断。在这种情况下,我使用Voip push to 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];
}
}
我将我的Login API请求中从didUpdatePushCredentials
收到的令牌发送到我的应用服务器。
我心中有以下疑问,并为他们寻求答案。
PushKit
是否同时需要APNS证书和Voip证书?或只是其中之一,以及哪一个,为什么?- 如果同时需要两个证书,我是否需要将两个证书都保留在应用服务器上才能将成功推送发送到我的应用?
- 应该在服务器端使用哪个证书来推送通知,该通知将从服务器端调用“ 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证书发送推送。但是失败了,因为它找不到任何相应的证书。
因此,我无法将要发送到服务器的令牌和将在服务器上用于发送推送的证书的组合。
似乎,您对APNS
和PushKit
感到困惑
您需要首先参考下图。
在通知服务器和Apple]之间建立连接Push Notification服务沙箱和生产环境向您的应用传递远程通知。使用HTTP / 2时,同一证书可用于传递应用程序通知,更新ClockKit并发症数据和警报传入的后台VoIP应用活动。您为每个应用程序都需要单独的证书分发。
这意味着单个证书对两个都起作用。
问题:1
PushKit是否同时需要APNS
证书和VOIP
证书?还是其中之一,为什么?为什么?
问题:2
如果同时需要两个证书,我是否需要将两个证书都保留在应用服务器上才能将成功推送发送到我的应用?
似乎是与证书路径有关的问题。保留现在的状态,您可以在php script
下方使用触发通知
<?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
命令触发推送