2016-08-10 3 views
0

사용자에게 알림으로 GCM 정보를 보내는 PHP 코드가 있습니다.이 코드는 괜찮습니다. 내 안드로이드 기기에서 수신하지만 소리와 배지는 작동하지 않습니다. 나는 이유를 모른다.소리를 사용하는 Android GCM 알림

배열에서 $msg 필드 '메시지 작업'만 다른 필드가 작동하지 않습니다.

어떻게 해결할 수 있습니까? 내 코드입니다.

<?php 

//Checking http request we are using post here 
if($_SERVER['REQUEST_METHOD']=='POST'){ 

//Getting api key 
$api_key = $_POST['apikey'];  

//Getting registration token we have to make it as array 
$reg_token = array($_POST['regtoken']); 

//Getting the message 
$message = $_POST['message']; 

//Creating a message array 
$msg = array 
(
    'message' => $message, 
    'title'  => 'Message from Simplified Coding', 
    'subtitle' => 'Android Push Notification using GCM Demo', 
    'tickerText' => 'Ticker text here...Ticker text here...Ticker text here', 
    'vibrate' => 1, 
    "sound"=> "default", 
    "badge"=> "2", 
    'largeIcon' => 'large_icon', 
    'smallIcon' => 'small_icon' 
); 

//Creating a new array fileds and adding the msg array and registration token array here 
$fields = array 
(
    'registration_ids' => $reg_token, 
    'data'   => $msg 
); 

//Adding the api key in one more array header 
$headers = array 
(
    'Authorization: key=' . $api_key, 
    'Content-Type: application/json' 
); 

//Using curl to perform http request 
$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send'); 
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)); 

//Getting the result 
$result = curl_exec($ch); 
curl_close($ch); 

//Decoding json from result 
$res = json_decode($result); 


//Getting value from success 
$flag = $res->success; 

//if success is 1 means message is sent 
if($flag == 1){ 
    //Redirecting back to our form with a request success 
    header('Location: index.php?success'); 
}else{ 
    //Redirecting back to our form with a request failure 
    header('Location: index.php?failure'); 
} 
} 
+0

장치가 기본 알림 소리로 설정 한대로 기본 소리가 작동해야합니다. 배지는 Android가 아닌 iOS 기기에서 사용할 수있는 기능입니다. –

답변

1

기존 사운드 파일이 있는지 확인하십시오.

Notification payload support에서 언급 한 바와 같이, sound 매개 변수는 장치가 알림을 수신 할 때 재생할 소리를 나타냅니다. 아이폰 OS 사운드 파일은 클라이언트 응용 프로그램의 주요 번들 또는 라이브러리/응용 프로그램의 데이터 컨테이너의 폴더를 소리에있을 수있는 반면

안드로이드 사운드 파일은 /res/raw/에 있어야합니다.

DEFAULT_SOUND을 사용하면 주어진 소리가 무시됩니다.

자세한 내용은 SO 게시물 (How to change notification sound by code in android?)에 나와있는 해결 방법을 확인하십시오. 희망이 도움이됩니다.