实施PushKit并testing开发行为

我想在我的应用程序(VoIP应用程序)中实施PushKit服务,但我有以下怀疑:我看到,我只能生成生产VoIP证书,它的工作原理,如果我尝试在开发设备上testingVoIP推送通知服务?

这是我的执行testing:

通过这3行代码,我可以在didUpdatePushCredentialscallback上获取推送令牌,以便将其保存到我的服务器中。

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

服务器端我生成一个“正常”有效载荷推送通知只有警报文本,我发送到存储到我的服务器的VoIP令牌。

我使用debugging日志的callback,但他们永远不会被调用!

 - (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设备令牌的推送通知消息,我从来没有通知didReceiveIncomingPushWithPayloadcallback,但从服务器我得到200 OK消息(消息已成功发送)

为了防止有人对使用Pushkittestingvoip推送通知感兴趣,我在这里留下了一个小程序:

1 – 如果您还没有,请创build一个带钥匙串访问的CSR ,并在本地保存您的CSR。

2 – 去苹果开发者并获得访问证书,标识符和configuration文件。 在会员中心。

  • 内部标识符 – >应用程序ID创build一个新的应用程序ID
  • 内部设备 – >所有添加设备UDID你想用于testingVoIP推送
  • 内部证书 – >全部创build一个新的生产证书:VoIP服务证书。 select先前为您的voip服务证书创build的应用程序ID。 select先前创build的CSR(证书签名请求),一旦创build,下载新的voip_services.cer

一旦下载双击voip_services.cer为了打开钥匙串访问应用程序和导出生成证书的私钥:右键导出certificate.p12文件。

voip_services.cercertificate.p12文件保存到一个文件夹中,以创build您的服务器推送通知生成器

最后再次进入Apple开发者网站,在Provisioning Profiles-> Distribution内部创build一个新的Ad-Hoc发行版configuration文件,其中包括您想要用于testingvoip推送的所有设备UDID。 下载此configuration文件并拖放到您的xcode,以便在您的应用程序上使用它。

现在让我们创build将接收voip推送通知的iOS应用程序:

  • 从Xcode新项目菜单中创build一个新的单一视图应用程序。
  • 在上一节中根据创build的应用程序ID填充其包标识符。
  • 在General-> Linked Frameworks和Libraries中添加PushKit.framework。
  • 在function中启用后台模式并selectIP语音选项。
  • 在“生成设置” – >“代码签名”中,select先前下载的configuration文件,然后select“分发为代码签名标识”。

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

在你的根视图控制器头部( ViewController.h )中为PushKit.framework导入:

 #import <PushKit/PushKit.h> 

添加委托以实现其function:

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

实施所需的委托function:

 - (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文件,以便安装在testing设备上(您可以使用例如Testflight来完成这项工作)。

执行它并从日志中获取我们将用于发送推送的PushCredentials。

现在让我们去服务器端(我遵循这个raywenderlich教程的伟大指南):

回到文件夹,你放置了三个文件:

  • voip_services.cer
  • certificate.p12

1 – 打开terminal,从证书文件创buildpem文件:

 #openssl x509 -in voip_services.cer -inform der -out PushVoipCert.pem 

2 – 从导出的私钥文件创buildpem文件:

 #openssl pkcs12 -nocerts -out PushVoipKey.pem -in certificate.p12 

3 – 将两个pem文件合并为一个:

 #cat PushVoipCert.pem PushVoipKey.pem > ck.pem 

为了发送推送,你可以使用从raywenderlich教程教程Pusher或使用简单的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由创buildPushVoipKey.pem时在步骤2中添加的密码

而已。 执行php脚本:

 #php simplePushScript.php 

你应该收到你的voip推送通知(你应该看到应用程序日志:“didReceiveIncomingPushWithPayload”)

testing后,我想知道如何通过pushkit框架接收标准的推送通知,但不幸的是我没有答案,因为当注册推式我找不到任何其他PKPushType但PKPushTypeVoIP …

 pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP]; 

就这样! 谢谢阅读!

今天我详细探讨了这一点。 我也想知道如何在开发版本上使用生成的推送令牌,当苹果只允许我们生成生产VoIP推送证书。

在服务器上,您必须将产品推送到gateway.push.apple.com并将开发/沙箱推送到gateway.sandbox.push.apple.com 。 我能够在gateway.sandbox.push.apple.com上使用生产VoIP证书生成和接收VoIP应用程序的开发版本。 我还没有尝试,但假设它也将工作在临时或生产构build和使用gateway.push.apple.com

另外请注意,推送通知在模拟器中根本不起作用。

即使不使用它们,也需要启用远程通知:

  • 在开发者平台上的App ID标识符中
  • 重新创build开发configuration文件
  • 点击XCode中的全部下载 – >首选项… – >帐户 – >您的帐户
  • 在function – >背景模式中启用远程通知

完成此操作后,您将在“debugging”和“发行版”中收到代理callback。