我在我们的系统中遇到一个奇怪的问题,即推送通知(对于iOS和Android)没有始终传送到移动设备,尽管我在交付时获得了成功响应这些消息(来自Apple和谷歌)。
后端是PHP。这是我用来在iOS中发送推送通知的代码。它返回成功:156 ,但推送不是始终。
同样对于iOS,有没有办法让我知道响应为0(失败)的原因?
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'pushcert.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', '');
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
if ($fp) {
$body['aps'] = array(
'alert' => $message,
'badge' => 1,
'sound' => 'default'
);
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
return "Success: $result";
}
else
{
return "Failed: $err $errstr";
}
以下是Android代码片段:
$apiKey = ANDROID_PUSH_API_KEY;
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
return $result;
对于Android,我确实得到了成功的回复,但有时会将消息传递给设备,有时不会: 的 {" multicast_id":5182490226054200549,"成功":1,"失效":0," canonical_ids":0,&# 34;结果":[{" MESSAGE_ID":" 0:1495238429549628%31ecb33bf9fd7ecd"}]}