【问题标题】:push notification provider built in php内置 php 的推送通知提供程序
【发布时间】:2012-07-09 21:55:21
【问题描述】:

我在开始时从互联网下载了一个用于苹果推送通知的 php 代码,当我将设备令牌值分配给变量 direct 时它是有效的,但是当我编辑此 php 代码以从数据库 MYSQL 中获取设备令牌值时它没有工作:

<?php
// Put your private key's passphrase here:
$passphrase = '123456';

// Put your alert message here:
$message = 'hi push message';

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

$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</br>' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);

// Encode the payload as JSON
$payload = json_encode($body);

////////////////////////////////////////////////////////////////////////
//make global array
$x = array();

function selectfromdb(){
    $con = mysql_connect("localhost","root","root");
    mysql_select_db("my_db", $con);
 mysql_query("set character_set_server='utf8'");
 mysql_query("set names 'utf8'");
 $result = mysql_query("SELECT idip FROM iphoneid");
$c = 0;
while($r = mysql_fetch_array($result))
{
$x[$c] = $r['idip'];
$c++;
}
}
///////////////////////////////////////////////////////////////////////
selectfromdb();
$i = 0;
while ($i < sizeof($x)){
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*',$x[$i]) . pack('n', strlen($payload)) .$payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));


if (!$result)
echo 'Message not delivered</br>' . PHP_EOL;
else
echo 'Message successfully delivered</br>' . PHP_EOL;

$i++;
}
echo 'end</br>';

// Close the connection to the server
fclose($fp);
?>

输出: 连接到 APNS 结束

【问题讨论】:

    标签: php iphone mysql ios push-notification


    【解决方案1】:

    看来你需要添加

    global $x; 顶部的 selectfromdb 函数。只是让它返回一个值可能会更好。

    function selectfromdb(){
        $x = array();
        $con = mysql_connect("localhost","root","root");
        mysql_select_db("my_db", $con);
        mysql_query("set character_set_server='utf8'");
        mysql_query("set names 'utf8'");
        $result = mysql_query("SELECT idip FROM iphoneid");
        $c = 0;
        while($r = mysql_fetch_array($result))
        {
            $x[$c] = $r['idip'];
            $c++;
        }
        return $x;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      • 2012-12-25
      相关资源
      最近更新 更多