推送ios push 代码

//init
public function getFp() {
if (!$this->fp) {
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', FILE.self: :iosLocalCert);
stream_context_set_option($ctx, 'ssl', 'passphrase', self: :iosPassphrase);

$this->fp = stream_socket_client(self: :iosPushUrl, $err, $errStr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$this->fp) {
sleep(5);
$this->fp = stream_socket_client(self: :iosPushUrl, $err, $errStr, 60, STREAM_CLIENT_CONNECT, $ctx);

if ($this->fp) {
ERROR_Errors: :getInstance()->getErrors(2001);
}
}
}

return $this->fp;

}

//send push
public function iosPush($registrationIds = array(), $content = '', $extras = array()) {
$this->getFp();
if ($this->fp) {
$extras['aps'] = array('alert' = >$content, 'sound' = >'default');

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

//消息发送失败记录
$noticeNotMsg = array();
foreach($registrationIds as $deviceToken) {
// Build the binary notification
$msg = chr(0).pack('n', 32).pack('H*', $deviceToken).pack('n', strlen($payload)).$payload;
// Send it to the server
fwrite($this->fp, $msg, strlen($msg));
}
return true;

} else {
//连接失败
}
return false;
}
Posted in : php


发表评论

电子邮件地址不会被公开。 必填项已用*标注