以防万一有人有兴趣使用 Pushkit 测试 voip 推送通知,我在这里留下了一个我成功遵循的小程序:
1 - 创建(如果您还没有)CSR使用钥匙串访问并在本地保存您的 CSR。
2 - 转到 Apple Developer 并获取访问证书、标识符和配置文件。在会员中心。
- Inside Identifiers-> App IDs 创建一个新的应用程序 ID
- 在“设备”->“全部”内添加要用于测试 voip 推送的设备 UDID
- 内部证书 -> 全部 创建新的生产证书:VoIP 服务证书。选择之前为您的 voip 服务证书创建的应用程序 ID。选择之前创建的 CSR(证书签名请求),创建后下载新的 voip_services.cer
下载后双击voip_services.cer为了打开钥匙串访问应用程序并导出生成证书的私钥:右键导出证书.p12 file.
Save voip_services.cer and 证书.p12文件放在文件夹中以创建服务器推送通知生成器
最后再次访问 Apple Developer 网站,在 Provisioning Profiles->Distribution 中创建一个新的临时分发配置文件包括您想要用于测试 voip 推送的所有设备 UDID。下载此配置文件并将其拖放到您的 xcode 中,以便在您的应用程序中使用它。
现在让我们创建将接收 voip 推送通知的 iOS 应用程序:
- 从 Xcode 新项目菜单创建一个新的单视图应用程序。
- 根据上一节中创建的应用程序 ID 填写其捆绑包标识符。
- 在“常规”->“链接的框架和库”中添加 PushKit.framework。
- 在功能中启用后台模式并选择 IP 语音选项。
- 在“构建设置”->“代码签名”中,选择您之前下载的配置文件,然后选择“分发”作为“代码签名身份”。
让我们在应用程序中添加代码Pasquale https://stackoverflow.com/users/2289654/pasquale在他的问题中添加:
在您的根视图控制器标头中(视图控制器.h) PushKit.framework 的导入:
#import <PushKit/PushKit.h>
添加委托以实现其功能:
@interface ViewController : UIViewController <PKPushRegistryDelegate>
在根视图控制器(ViewController.m)的 viewDidLoad 函数中添加推送注册:
- (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 教程 http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1):
返回到放置三个文件的文件夹:
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 https://github.com/noodlewerk/NWPusher from raywenderlich 教程 http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1教程或使用简单的 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 为您在创建 PushVoipKey.pem 时在步骤 2 中添加的密码
就是这样。执行php脚本:
#php simplePushScript.php
并且您应该收到 voip 推送通知(您应该看到应用程序日志:“didReceiveIncomingPushWithPayload”)
在那次测试之后,我想知道如何通过pushkit框架接收标准推送通知,但不幸的是我没有答案,因为在注册推送类型时我找不到任何其他PKPushType,但PKPushTypeVoIP...
pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
就这样!谢谢阅读!