我正在尝试使用php将通知发送到Android应用程序,并且在没有声音的情况下工作正常。我收到预期的前台和后台通知。
这是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
错误。
$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"
答案 0 :(得分:1)
根据我的评论,您必须按照以下方式使用JSON。
解决方案: JSON中消息的外观表明您正在使用HTTP v1 API。您链接的文档适用于旧版API。
用于向Android和iOS设备发送带有声音的通知的HTTP v1 API JSON应该为:
{
"message":{
"token":"your-token-value",
"notification":{
"title":"Test",
"body":"Test message from server"
},
"android":{
"notification":{
"sound":"default"
}
},
"apns":{
"payload":{
"sound":"default"
}
}
}
}
参考链接为:Unable to add sound to notification payload
谢谢。
答案 1 :(得分:0)
尝试一下:
{
"message":{
"token":"your-token-value",
"notification":{
"title":"Test",
"body":"Test message from server"
},
"android":{
"notification":{
"sound":"default"
}
},
"apns":{
"payload":{
"sound":"default"
}
}
}
}