Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php iOS发送多个推送通知_Php_Mysql_Ios_Notifications_Push - Fatal编程技术网

Php iOS发送多个推送通知

Php iOS发送多个推送通知,php,mysql,ios,notifications,push,Php,Mysql,Ios,Notifications,Push,我想用php脚本从数据库发送多个PN。表中有3个设备令牌,但脚本仅将其发送到第一个设备令牌 这里的脚本,有人有一个想法 $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'DEV.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); $fp = stream_socket_clien

我想用php脚本从数据库发送多个PN。表中有3个设备令牌,但脚本仅将其发送到第一个设备令牌

这里的脚本,有人有一个想法

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

$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);
}

$ausgabe.= 'Connected to APNS' . PHP_EOL . '<hr>';

$body['aps'] = array(
    'alert' => $message,
    'sound' => 'note.wav',
    'badge' => 1
);

$payload = json_encode($body);

$selectSQL = 'SELECT * from app_pushnotification';
$result = mysql_query($selectSQL);  
while($row = mysql_fetch_array($result)){
    $i+=1;
    $ausgabe.= $i.' - '. $row['device_token'].' ';
    $deviceToken = $row['device_token'];

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

    $result = fwrite($fp, $msg, strlen($msg));

    if (!$result){
        $ausgabe.='Message to not delivered' . PHP_EOL . '<hr>';
    } else {
        $ausgabe.='Message to successfully delivered' . PHP_EOL . '<hr>';
    }

}

$ausgabe.='close connection';
fclose($fp);

} 
echo $ausgabe;
$ctx=stream\u context\u create();
stream_context_set_选项($ctx,'ssl','local_cert','DEV.pem');
stream_context_set_选项($ctx,'ssl','passphrase',$passphrase);
$fp=流\u套接字\u客户端(
'ssl://gateway.sandbox.push.apple.com:2195“,$err,
$errstr,60,流式_客户端_连接|流式_客户端_持久,$ctx);
如果(!$fp){
退出(“连接失败:$err$errstr.PHP_EOL”);
}
$ausgabe.=“已连接到APN”。PHP_EOL
'; $body['aps']=数组( “警报”=>$message, “声音”=>“note.wav”, “徽章”=>1 ); $payload=json_encode($body); $selectSQL='SELECT*from app_pushnotification'; $result=mysql\u查询($selectSQL); while($row=mysql\u fetch\u数组($result)){ $i+=1; $ausgabe.=$i.-'.$row['device_token'].'; $deviceToken=$row['device_token']; $msg=chr(0).pack('n',32).pack('H*',$deviceToken).pack('n',strlen($payload))。$payload; $result=fwrite($fp,$msg,strlen($msg)); 如果(!$result){ $ausgabe.='messageto not delivered'.PHP_EOL.'; }否则{ $ausgabe.='messageto successfully delivered'.PHP_EOL.'; } } $ausgabe.=‘紧密联系’; fclose($fp); } echo$ausgabe;

问候Kevin

您的错误是更改循环中
$result
的值。因此,脚本无法获取下一行数组

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

$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);
}

$ausgabe.= 'Connected to APNS' . PHP_EOL . '<hr>';

$body['aps'] = array(
    'alert' => $message,
    'sound' => 'note.wav',
    'badge' => 1
);

$payload = json_encode($body);

$selectSQL = 'SELECT * from app_pushnotification';
$result = mysql_query($selectSQL);  
while($row = mysql_fetch_array($result)){
    $i+=1;
    $ausgabe.= $i.' - '. $row['device_token'].' ';
    $deviceToken = $row['device_token'];

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

    $write_result = fwrite($fp, $msg, strlen($msg));

    if (!$write_result){
        $ausgabe.='Message to not delivered' . PHP_EOL . '<hr>';
    } else {
        $ausgabe.='Message to successfully delivered' . PHP_EOL . '<hr>';
    }

}

$ausgabe.='close connection';
fclose($fp);

} 
echo $ausgabe;
$ctx=stream\u context\u create();
stream_context_set_选项($ctx,'ssl','local_cert','DEV.pem');
stream_context_set_选项($ctx,'ssl','passphrase',$passphrase);
$fp=流\u套接字\u客户端(
'ssl://gateway.sandbox.push.apple.com:2195“,$err,
$errstr,60,流式_客户端_连接|流式_客户端_持久,$ctx);
如果(!$fp){
退出(“连接失败:$err$errstr.PHP_EOL”);
}
$ausgabe.=“已连接到APN”。PHP_EOL
'; $body['aps']=数组( “警报”=>$message, “声音”=>“note.wav”, “徽章”=>1 ); $payload=json_encode($body); $selectSQL='SELECT*from app_pushnotification'; $result=mysql\u查询($selectSQL); while($row=mysql\u fetch\u数组($result)){ $i+=1; $ausgabe.=$i.-'.$row['device_token'].'; $deviceToken=$row['device_token']; $msg=chr(0).pack('n',32).pack('H*',$deviceToken).pack('n',strlen($payload))。$payload; $write_result=fwrite($fp,$msg,strlen($msg)); 如果(!$write\u结果){ $ausgabe.='messageto not delivered'.PHP_EOL.'; }否则{ $ausgabe.='messageto successfully delivered'.PHP_EOL.'; } } $ausgabe.=‘紧密联系’; fclose($fp); } echo$ausgabe;

请注意,第二个
$result=
现在是
$write\u result=
您可以创建守护进程/cronjob来发送推送通知。它将帮助您在不冻结UI的情况下发送异步和多条消息。参考链接:

尝试对每条消息使用一个连接。关闭/打开循环中的连接。@kdogisthebest这不是个好主意。太慢了!Kevin,查询返回多少行?您确定错误不在数据库中,而不是在APNS服务器上?数据库返回所有3个令牌,如果我不发送消息,它是否会为所有3个令牌输出“Message to successfully delivered”?