【问题标题】:How to setup push notification for development?如何为开发设置推送通知?
【发布时间】:2013-03-18 12:41:30
【问题描述】:

我为 ios 开发了一个应用程序,它已在应用程序商店上发布。 推送通知系统在开发中运行良好,但现在,我根本没有收到任何通知。 在发布之前,我已经生成了与启用推送并配置了生产推送 SSL 证书的应用程序 ID 关联的开发配置文件。我已经下载了生产推送 SSL 证书并将其安装在我的钥匙串访问中,将其及其私钥导出,将它们转换为 pem 并将它们合并到一个唯一的 pem 文件中,我将其上传到我的服务器,其中包含发送的 php 脚本通知。 我已将我的 php 脚本中的服务器更改为生产服务器(ssl://gateway.push.apple.com:2195)。 脚本似乎仍然发送通知并且没有触发任何错误。

我错过了什么?

这是我的 php 脚本:

<?PHP
/* Here there's Code to retrieve device tokens and put it in a variable $deviceToken from     my online database and then...*/


$message = "Message to be sent";    
$payload = '{
                "aps" : 

                    { "alert" : "'.$message.'",
                      "badge" : 1,
                      "sound" : "bingbong.aiff"
                    } 
            }';

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'secret');

// se l'app è già sull'appstore togliere sandbox e lasciare solo gateway.push.apple.com:2195
// invece di gateway.sandbox.push.apple.com:2195
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if(!$fp){
    print "Failed to connect $err $errstrn";
    return;
} else {
    print "Notifications sent! </br>";
}

foreach($devArray as $deviceToken){
    $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack        ("n",strlen($payload)) . $payload;
    print "sending message :" . $payload . "n to device:".$deviceToken."</br>";
    fwrite($fp, $msg);
}
fclose($fp);

?>

【问题讨论】:

  • 您的应用程序是否已使用生产配置进行编译?你没有提到它
  • 可能 deviceTokens 不正确。你检查过print "sending message :" . $payload . "n to device:".$deviceToken."&lt;/br&gt;"; 吗?
  • 是的,已使用生产配置进行编译,并在应用商店发布。我虽然关于令牌。您引用的行报告行,例如“sending message :{ "aps" : { "alert" : "message", "badge" : 1, "sound" : "bingbong.aiff" } }n to device:c016118d79b914dee92d34b66aa334f4ce2a29ccf77026046638632e61b7538d 和 all好像还可以……
  • 设备令牌不是有一些空格吗?我不确定,但我相信发送通知会有所不同。
  • 不,我修剪它以避免这种情况

标签: ios objective-c push-notification


【解决方案1】:

因为您必须使用有效的“生产推送 SSL 证书”而不是“开发推送 SSL 证书”重新生成 PEM。

编辑:对不起,我读错了两次。该程序看起来不错,我发布了我一直使用的 php 代码,它看起来非常相似,但您可以尝试编辑它。

<?php
      // Passphrase for the private key (ck.pem file)
      // $pass = ”;
      // Get the parameters from http get or from command line
      $message = 'Testo Notifica Push';
      $badge = 1;
      $sound = 'default';

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

      // Construct the notification payload
      $body = array();
      $body['aps'] = array('alert' => $message);

      if ($badge)
            $body['aps']['badge'] = $badge;
      if ($sound)
            $body['aps']['sound'] = $sound;

      /* End of Configurable Items */

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

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

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'cert.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,
    $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;

// Construct the notification payload
      $body = array();
      $body['aps'] = array(’alert’ => $message);

      if ($badge)
            $body['aps']['badge'] = $badge;
      if ($sound)
            $body['aps']['sound'] = $sound;

// 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 脚本是否触发了一些错误
  • 不幸的是我没有运气^^
  • 完全没有错误?出现“已连接到 APNS。消息已成功传递”?
  • 如果您从数据库中检索令牌,请检查 TRIM 字符串并检查它是否正确,因为它可以从开发模式和生产模式更改。
  • 不,我不认为这可能是一个问题,因为我处于同样的情况并且一切正常。服务器上的 SSL 协议工作正常吗?开发阶段也一样吗?我问这个是因为 italt 中的主要托管解决方案不允许 ssl 协议或每次调用 2195 端口(如 Aruba 和 Register)。
【解决方案2】:

设置推送时要做的最后一步是确保您没有混合生产和开发设备令牌! 就我而言,在我的数据库中,推送发送到的第一个设备令牌是开发设备令牌。发生这种情况时,Apple 服务器会终止连接,避免发送任何其他通知。感谢苹果!

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 2021-09-20
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    相关资源
    最近更新 更多