如何设置推送通知进行开发?

时间:2013-03-18 12:41:30

标签: ios objective-c push-notification

我为ios开发了一个应用程序,它已在应用商店发布。 推送通知系统在开发中运行良好但现在,我根本没有收到任何通知。 在发布之前,我已经生成了与应用ID相关联的开发配置文件,该应用ID已启用推送并配置了生产推送SSL证书。我已经下载了生产推送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);

&GT;

2 个答案:

答案 0 :(得分:1)

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

编辑:抱歉,我读错了两次。程序看起来不错,我发布了我一直使用的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);
?>

答案 1 :(得分:0)

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