ios - Apple 推送通知在生产环境中不起作用

标签 ios iphone push-notification apple-push-notifications

我的应用程序在 App Store 上,推送通知不起作用。 我的应用程序处于开发阶段,推送通知可以正常工作。

我想我应该通过 Ad Hoc 部署进行测试。不管怎样,这就是我所知道的...

应用 ID

我的 App ID 是 com.MyName.My-App

它为开发和分发启用了推送通知

enter image description here

APNs 证书

我同时拥有开发和分发证书。这是我关心的 Dist。

enter image description here

它的名字是 com.MyName.My-App

导出到 PEM

我已经选择了证书和私钥,并将其导出,如下所示:

enter image description here

密码保护它。

然后我跑了

openssl pkcs12 -in Certificates.p12 -out pushcert.pem -nodes -clcerts

提供密码,并成功获得 pushcert.pem 输出。

正在下载应用

我通过服务器端设备 token 为我的设备清除,我从应用商店下载我的应用程序,打开它并接受推送通知,然后登录到我的服务器以检查我的设备 token 。我现在有了我的生产设备 token 。我运行了这个简单的 php 脚本(当我提供我的开发设备 token 时它可以工作)但是我的生产设备 token 失败了。

<?php

// Put your device token here (without spaces):
$deviceToken = 'myProductionDeviceTokenInHere';

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

// Put your alert message here:
$message = 'Test';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'pushcert.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
stream_context_set_option($ctx, 'ssl', 'cafile', 'entrust_2048_ca.cer');

// Open a connection to the APNS server
$fp = stream_socket_client(
    'ssl://gateway.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);

为什么会失败?它适用于我的开发设备 token ,但不适用于我的生产设备 token 。我是不是做错了什么?

最佳答案

发生的情况是您可能使用错误的配置文件进行分发。 为了在最终版本中接收推送通知,您需要安装最新的配置文件,您已经在其中启用了推送通知以进行分发。这是最常见的错误,我过去遇到过一两次。

所以现在您必须执行以下步骤:

  • 登录苹果开发者中心
  • 为此 App id 创建一个新的 Provisioning Profile 以供分发 Screenshot
  • 输入所有必要的详细信息,继续进行全面设置
  • 下载配置文件并双击进行安装
  • 在您的项目中,Build Settings-> Code Signing -> Distribution 选择您新创建的配置文件
  • 现在推送通知也将在分发版本中到达最终用户。

希望对您有所帮助,朱利安。

关于ios - Apple 推送通知在生产环境中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34302640/

相关文章:

iphone - 如何清除UIWebView中的Cookies

ios - Timer.TimerPublisher 在滚动时不会触发

iphone - iOS 崩溃报告 "Hardware Model"到底是什么意思?

iphone - 是否可以从货币代码中导出货币符号?

iphone - 展开和折叠 UITableview

ios - 应用程序首次启动时需要多长时间才能收到 APNS Push_token?

android - 如何更新 Android 中的现有通知?

php - 推送通知 Drupal 模块 - 删除设备(Web 服务)

ios - NavigationBar 内容在 popresStatusBarHidden = YES 时从 View 中消失

iphone - 如何使用包含彩色图像的按钮自定义 UIToolbar?