FCM Android通知声音问题 [英] FCM Android Notification sound issue
问题描述
我正在尝试使用php将通知发送到Android应用程序,并且在没有声音的情况下工作正常.我收到预期的前台和后台通知.
I am trying to send notification using php to Android Application, and which is working fine without sound. I am receiving the notification both foreground and background as expected.
这是PHP代码,
<?php
$token = $_GET['token'];
$action = $_GET['action'];
$msgTitle = $_GET['msgTitle'];
$msgDescription = $_GET['msgDescription'];
$notificationTitle = $_GET['notificationTitle'];
require './google-api-php-client-2.2.2/vendor/autoload.php';
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setAuthConfig('./testPrjoectAPP-firebase-adminsdk-9hn21-22c1b3f426.json');
$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
$httpClient = $client->authorize();
$project = "testPrjoectAPP";
$message = [
"message" => [
"notification" => [
"body" => "Message FCM",
"title" => $notificationTitle
],
"token" => $token,
"data" => [
"action" => $action,
"msgTitle" => $msgTitle,
"msgDescription" => $msgDescription
]
]
];
$response = $httpClient->post("https://fcm.googleapis.com/v1/projects/{$project}/messages:send", ['json' => $message]);
echo$response->getReasonPhrase(); // OK
?>
但是当我将声音参数添加到通知有效负载并执行php时,我从php中收到Bad Request
错误.
But when I add sound parameter to notification payload and execute php I am getting Bad Request
error from php.
.
$message = [
"message" => [
"notification" => [
"body" => "Message FCM",
"title" => $notificationTitle,
"sound" => "default"
],
// Send with token is not working
"token" => $token,
"data" => [
"action" => $action,
"msgTitle" => $msgTitle,
"msgDescription" => $msgDescription
]
]
];
修改
这是我使用
data: "{\n \"error\": {\n \"code\": 400,\n \"message\": \"Invalid JSON payload received. Unknown name \\\"sound\\\" at 'message.notification': Cannot find field.\",\n \"status\": \"INVALID_ARGUMENT\",\n \"details\": [\n {\n \"@type\": \"type.googleapis.com/google.rpc.BadRequest\",\n \"fieldViolations\": [\n {\n \"field\": \"message.notification\",\n \"description\": \"Invalid JSON payload received. Unknown name \\\"sound\\\" at 'message.notification': Cannot find field.\"\n }\n ]\n }\n ]\n }\n}\n"
推荐答案
根据我的评论,您必须按照以下方式使用JSON.
As per my comment, You have to use your JSON like following way.
解决方案: JSON中消息的外观表明您正在使用HTTP v1 API.您链接的文档适用于旧版API.
Solution: The appearance of message in your JSON indicates you are using the HTTP v1 API. The documentation you linked is for the legacy API.
用于向Android和iOS设备发送带声音的通知的HTTP v1 API JSON应该为:
The HTTP v1 API JSON to send a notification with sound for Android and iOS devices should be:
{
"message":{
"token":"your-token-value",
"notification":{
"title":"Test",
"body":"Test message from server"
},
"android":{
"notification":{
"sound":"default"
}
},
"apns":{
"payload":{
"sound":"default"
}
}
}
}
参考链接为:无法向通知有效内容添加声音
谢谢.
这篇关于FCM Android通知声音问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!