我有一个推送通知的应用程序集。 Android代码工作正常,我收到推送通知。对于Apple来说,手机只是振动并为推送通知发出声音,但我没有获得徽章或其他任何东西。
有人可以告诉我哪里出错了吗?
<?php
// function to send Android push notification
function send_message($deviceToken, $message){
$messageArr['message'] = "";
if($message=="")
{
$messageArr['message'] = "Test data";
}
else
{
$messageArr['message'] = $message ;
}
$registatoin_ids = $deviceToken;
return mer_send_notification($registatoin_ids, $messageArr);
}
function mer_curlPost($url, $headers, $fields) {
$ch = curl_init();
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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
function mer_send_notification($registatoin_ids, $message) {
$url = 'https://android.googleapis.com/gcm/send';
$apiKey = 'mykeyishere';
echo 'Test if 4\n';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
$result = mer_curlPost($url, $headers, $fields);
return $result;
}
function sendNotification($dataArr, $device_token_array) {
$apns_url = NULL;
$apns_cert = NULL;
$apns_port = 2195;
$development = false;
if ($development) {
$apns_url = 'gateway.sandbox.push.apple.com';
$apns_cert = 'devcert.pem';
} else {
$apns_url = 'gateway.push.apple.com';
$apns_cert = 'prodcert.pem';
}
$stream_context = stream_context_create();
stream_context_set_option($stream_context, 'ssl', 'local_cert', $apns_cert);
stream_context_set_option($stream_context, 'ssl', 'passphrase', 'mypassphrase');
$payload = array();
$payload['aps'] = $dataArr;
$payloadJson = json_encode($payload);
$apns = @stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);
foreach ($device_token_array as $key => $device_token) {
$apns_message = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device_token)) . chr(0) . chr(strlen($payloadJson)) . $payloadJson;
$xxx[] = @fwrite($apns, $apns_message);
}
$yyy = @socket_close($apns);
$zzz = @fclose($apns);
$arr = array();
$arr[] = $stream_context;
$arr[] = $apns;
$arr[] = $xxx;
$arr[] = $yyy;
$arr[] = $zzz;
return $arr;
}
答案 0 :(得分:0)