通过利用API推送通知 [英] Using push notification using Api

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

问题描述

我试图使用API​​来使用推送通知,我没有得到没有我得到任何回应任何错误消息。

我已签<一个href=\"http://stackoverflow.com/questions/3891118/apple-push-notification-service-with-php-script/17723596#17723596\">Apple用PHP脚本推送通知服务

和应用在我的$ C $的变化C.因此,但仍然没有工作。

我不能够得到如何获得 SERVERID ,我在使用

  $设备='fbb5a9c71066794d57fee33b4005a89f1bb8941a68660fd6e91f466be1299ab6'; //我的iPhone deviceToken
$有效载荷['APS'] =阵列(
    '警报'=&GT; 这是警报文本,
    '徽章'=&GT; 1,
    声音= GT; '默认'
);$有效载荷['服务器'] =阵列(
    SERVERID'=&GT; 1,
    '名'=&GT; keyss.in
);$有效载荷= json_en code($负载);$ 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',$错误,$为errorString,2,STREAM_CLIENT_CONNECT,$ streamContext);$ apnsMessage = CHR(0)。字符(0)。字符(32)。包(H *',str_replace函数('','',$设备))。字符(0)。字符(strlen的($有效载荷))。 $有效载荷;
FWRITE($ APNS,$ apnsMessage);// socket_close($ APNS);似乎是错在这里...
FCLOSE($ APNS);

获取错误:


  

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


  
  

警告:FWRITE()预计参数1是资源,鉴于布尔


  
  

警告:FCLOSE()预计参数1是资源,鉴于布尔



解决方案

由于您使用的是旧的二进制格式的通知,你没有得到任何回复:

  $ apnsMessage = CHR(0)。字符(0)。字符(32)。包(H *',str_replace函数('','',$设备))。字符(0)。字符(strlen的($有效载荷))。 $有效载荷;

为了得到反应(反应只在一个错误的情况下返回),使用增强的格式为:

  $ apnsMessage =包(C,1)。包(N,$ apple_identifier)。包(N,$ apple_expiry)。包(N,32)。包(H *',str_replace函数('','',$ deviceToken))。包(N的strlen($有效载荷))。 $有效载荷;

您可以看到样本code <一个href=\"http://learn-php-by-example.blogspot.com.es/2013/01/working-with-apple-push-notification.html\">here.

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

I have checked Apple Push Notification Service with PHP Script

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

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

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

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;

You can see sample code here.

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

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