使用声音的Android GCM通知

时间:2016-08-10 19:37:13

标签: php google-cloud-messaging android-notifications

我有一个PHP代码,发送到GCM信息通知用户,这个代码发送确定,我收到我的Android设备,但声音和徽章不起作用。我不知道为什么。

在阵列$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');
}
}

1 个答案:

答案 0 :(得分:1)

请检查您是否有现有声音文件。

Notification payload support中所述,sound参数表示设备收到通知时播放的声音。

  

Android声音文件必须位于/res/raw/,而iOS声音文件可以位于客户端应用的主捆绑中,也可以位于应用数据容器的Library / Sounds文件夹中。

请注意,使用DEFAULT_SOUND会忽略任何给定的声音。

有关其他信息,请查看此SO帖子中提供的解决方案 - How to change notification sound by code in android?。希望它有所帮助。