FCM Android 通知声音问题

Cod*_*ezk 2 php android firebase firebase-cloud-messaging

我正在尝试使用 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

?>
Run Code Online (Sandbox Code Playgroud)

但是,当我将声音参数添加到通知有效负载并执行 php 时,我收到Bad Request来自 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
         ]


    ]
];
Run Code Online (Sandbox Code Playgroud)

编辑

这是我在打印时收到的错误消息

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"
Run Code Online (Sandbox Code Playgroud)

Pra*_*ani 6

根据我的评论,您必须像以下方式一样使用您的 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"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

参考链接是:无法向通知负载添加声音

谢谢你。