使用 Api 使用推送通知 [英] Using push notification using Api

查看:29
本文介绍了使用 Api 使用推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Api 使用推送通知,但没有收到任何错误消息,也没有收到任何响应.

I am trying to use push notification using Api and I am not getting any error message neither I am getting any response.

我已经检查了 使用 PHP 脚本的 Apple 推送通知服务

并相应地对我的代码进行了更改,但仍然无法正常工作.

and applied changes in my code accordingly but still not working.

我不知道如何获取我必须在其中使用的 serverId

I am not able to get how to get serverId that I have to use in

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

获取错误:

警告:stream_socket_client():无法连接到 ssl://gateway.sandbox.push.apple.com:2195(连接超时)

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out)

警告:fwrite() 期望参数 1 是资源,给定的布尔值

Warning: fwrite() expects parameter 1 to be resource, boolean given

警告:fclose() 期望参数 1 是资源,给定的布尔值

Warning: fclose() expects parameter 1 to be resource, boolean given

推荐答案

您没有得到任何响应,因为您使用的是旧的二进制通知格式:

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;

您可以在此处查看示例代码.

这篇关于使用 Api 使用推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆