简体   繁体   English

使用Api使用推送通知

[英]Using push notification using Api

I am trying to use push notification using Api and I am not getting any error message neither I am getting any response. 我正在尝试通过Api使用推式通知,但没有收到任何错误消息,也没有收到任何响应。

I have checked Apple Push Notification Service with PHP Script 我已经用PHP脚本检查了Apple Push Notification Service

and applied changes in my code accordingly but still not working. 并相应地在我的代码中应用了更改,但仍然无法正常工作。

I am not able to get how to get serverId that I have to use in 我无法获得如何使用必须使用的serverId

$device = 'fbb5a9c71066794d57fee33b4005a89f1bb8941a68660fd6e91f466be1299ab6'; // My iphone deviceToken
$payload['aps'] = array(
    'alert' => 'This is the alert text',
    'badge' => 1,
    'sound' => 'default'
);

$payload['server'] = array(
    'serverId' => 1,
    'name' => 'keyss.in'
);

$payload = json_encode($payload);

$apnsCert = 'apple_push_notification_production.pem';

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

$apns = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);

$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);

//socket_close($apns); seems to be wrong here ...
fclose($apns);

Getting the errors: 得到错误:

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) 警告:stream_socket_client():无法连接到ssl://gateway.sandbox.push.apple.com:2195(连接超时)

Warning: fwrite() expects parameter 1 to be resource, boolean given 警告:fwrite()期望参数1为资源,给定布尔值

Warning: fclose() expects parameter 1 to be resource, boolean given 警告:fclose()期望参数1为资源,给定布尔值

You are not getting any response because you are using the old binary notification format : 您没有收到任何响应,因为您使用的是旧的二进制通知格式:

$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device)) . chr(0) . chr(strlen($payload)) . $payload;

In order to get responses (responses are returned only in case of an error), use the enhanced format : 为了获得响应(仅在发生错误时才返回响应),请使用增强的格式:

$apnsMessage = pack("C", 1) . pack("N", $apple_identifier) . pack("N", $apple_expiry) . pack("n", 32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n", strlen($payload)) . $payload;

You can see sample code here . 您可以在此处查看示例代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM