php - 苹果推送 PHP : No all notifications received

标签 php mysql ios apple-push-notifications

我有一个 PHP 脚本和一个用于 Apple 推送通知的 MySQL 数据库。该脚本有效,但并非所有通知都会收到通知。怎么了?

<?PHP

$db_user = "xx_fw"; // Gebruiker voor MySQL
$db_pass = "xxx"; // Wachtwoord voor MySQL
$db_host = "localhost"; // Host voor MySQL; standaard localhost
$db_db = "xx_fw"; // Database

// Als je al ergens anders een database connectie hebt gemaakt,
// maak dan van de volgende twee regels commentaar (# of // ervoor zetten)
mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_db);

$query = mysql_query("SELECT * FROM iospush");

                                $deviceToken = ARRAY();
while ($row = mysql_fetch_array($query)) {
  $deviceToken[] = $row["token"];
}

if($_POST['politie']){

    $message = stripslashes($_POST['message']);
    $politiemessage = "POLITIEBERICHT: ".$message;

    $payload = '{
                    "aps" : 

                        { "alert" : { "body" : "'.$politiemessage.'" },
"badge" : 1,
"sound" : "politiepush.wav"
                        } 
                }';


    $ssl='ios-ssl.pem';

    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', $ssl);
    stream_context_set_option($ctx, 'ssl', 'passphrase', 'xxx');
    $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!";
    }

    $devArray = array();
    $devArray[] = $deviceToken;

    foreach($deviceToken as $token){
    $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $token)) . pack        ("n",strlen($payload)) . $payload;
        print "sending message :" . $payload . "n";

        fwrite($fp, $msg);
echo "<br><br>";
echo "Verstuurd aan: ".$token;
echo "<br><br>";

    }
    fclose($fp);

} else if($_POST['message']){

    $message = stripslashes($_POST['message']);

    $payload = '{
                    "aps" : 

                        { "alert" : { "body" : "'.$message.'" },
"badge" : 1,
"sound" : "chime"
                        } 
                }';


    $ssl='ios-ssl.pem';

    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', $ssl);
    stream_context_set_option($ctx, 'ssl', 'passphrase', 'xxx');
    $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!";
    }

    $devArray = array();
    $devArray[] = $deviceToken;

    foreach($deviceToken as $token){
    $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $token)) . pack        ("n",strlen($payload)) . $payload;
        print "sending message :" . $payload . "n";

        fwrite($fp, $msg);
echo "<br><br>";
echo "Verstuurd aan: ".$token;
echo "<br><br>";

    }
    fclose($fp);
}
?>

问题是发送推送的方式,还是 Apple 方面出了问题? 谢谢!

最佳答案

试试下面的代码:

                    $body['aps'] = array('alert' => $message);
                    if ($badge)
                    $body['aps']['badge'] = $badge;
                    if ($sound)
                    $body['aps']['sound'] = $sound;
                    /* End of Configurable Items */

                    $ctx = stream_context_create();
                    $a = stream_context_set_option($ctx, 'ssl', 'local_cert',  dirname(__FILE__).'\push.pem');

                    // assume the private key passphase was removed.
                    // stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
                    $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
                    //$status = array();
                    $status = '';
                    if ($fp) {
                         $status =  "Notification sent successfully."; //$message;
                    } else {
                    $status = "Failed to sent notification.";
                    //
                    }

                    $payload = json_encode($body);

                    $msg = @(chr(0) . pack("n",32) . pack('H*', str_replace(' ','' , $deviceToken)) . pack("n",strlen($payload)) . $payload);

                    if($msg)
                    {
                        fwrite($fp, $msg);
                        if (!extension_loaded('openssl')) {
                        }
                    }
                    echo $status;
?>

注意#请为 ios 使用正确的 .pem 文件

关于php - 苹果推送 PHP : No all notifications received,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25380188/

相关文章:

php - 获取按天分组的支付现金金额

php - 我如何使用 PHP 从 mysql 数据库中检索和显示时间戳?

ios - 是否建议在 iPhone X 虚拟主页按钮旁边放置一个按钮?

ios - UICollectionView 中带有 endItemAnimations 的断言失败

iphone - 在 iPhone 5 中自动调整 UIView 的大小

php - Zend 安全 SQL 查询?

php - 如何使用 PHP 获取 MySQL 数据库表中的最后一条记录?

php - PHP session 的 cookie 安全吗?

javascript - 从网页中的 2 张图片创建新图片

mysql - 在 SQL 的 where 子句中使用 case/if