2014-04-23 3 views
0

푸시 알림을 구현 한 애플리케이션을 만들었지 만 관리자 패널을 통해 알림을 보내면 알림 경고가 내 장치에서 두 번 울립니다. 별도의 프로필과 인증서 및 ck.pem 파일을 만들었습니다.푸시 알림 경고가 두 번 발생합니다.

도와주세요.

========

function sendnote_ios($message,$registatoin_ids_arr_ios){ 



// Put your private key's passphrase here: 
$passphrase = 'pushchat'; 

// Put your alert message here: 
$message = $message; 

$ctx = stream_context_create(); 
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); 
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 

// Open a connection to the APNS server 
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err, 
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 

if (!$fp) 
exit("Failed to connect: $err $errstr" . PHP_EOL); 

//echo 'Connected to APNS' . PHP_EOL; 

// Create the payload body 
$body['aps'] = array(
'alert' => $message, 
'sound' => 'default' 
); 
//echo '<pre>';print_r($body); 
// Encode the payload as JSON 
$payload = json_encode($body); 

// Build the binary notification 
foreach($registatoin_ids_arr_ios as $token){ 
$msg = chr(0) . pack('n', 32) . pack('H*', trim($token)) . pack('n', strlen($payload)) . $payload; 
//$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; 
$result = fwrite($fp, $msg, strlen($msg)); 
} 

// Send it to the server 
$result = fwrite($fp, $msg, strlen($msg)); 

/* 
if (!$result) 
echo 'Message not delivered' . PHP_EOL; 
else 
echo 'Message successfully delivered' . PHP_EOL; 
*/ 
// Close the connection to the server 
fclose($fp); 
} 
+0

공유하는 코드. –

답변

1

의 시도하자 푸시 알림의 서버 측 구현에서

<?php 
//your device token 
$deviceToken = ''; 

// Put your private key's passphrase here: 
$passphrase = ''; 

// Put your alert message here: 
$message = 'My first push notification!'; 

//////////////////////////////////////////////////////////////////////////////// 

$ctx = stream_context_create(); 
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); 
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 

// Open a connection to the APNS server 
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err, 
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 

if (!$fp) 
exit("Failed to connect: $err $errstr" . PHP_EOL); 

echo 'Connected to APNS' . PHP_EOL; 

// Create the payload body 
$body['aps'] = array(
'alert' => $message, 
'sound' => 'default' 
); 

// Encode the payload as JSON 
$payload = json_encode($body); 

// Build the binary notification 
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) .  $payload; 

// Send it to the server 
$result = fwrite($fp, $msg, strlen($msg)); 

if (!$result) 
echo 'Message not delivered' . PHP_EOL; 
else 
echo 'Message successfully delivered' . PHP_EOL; 

// Close the connection to the server 
fclose($fp);