douji2283 2016-08-10 19:37
浏览 32
已采纳

使用声音的Android GCM通知

I have a php code that send to GCM information to notification to user, this code send ok and I receive in my android device, but the sound and badge don't worked. I don't know why.

On array $msg only a field 'message work', other fields don't work.

How can I fix this? This is my code:

<?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条回答 默认 最新

  • dtgr3392 2016-08-11 16:09
    关注

    Please check if you have existing sound files.

    As mentioned in Notification payload support, sound parameter indicates a sound to play when the device receives the notification.

    Android sound files must reside in /res/raw/, while iOS sound files can be in the main bundle of the client app or in the Library/Sounds folder of the app’s data container.

    Please also note that using DEFAULT_SOUND will ignore any given sound.

    For additional information, please check solution given in this SO post - How to change notification sound by code in android?. Hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加