CodeDezk
CodeDezk

Reputation: 1260

FCM Android Notification sound issue

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.

Here is the PHP code,

<?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

?>

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
         ]


    ]
];

Edit

Here is the error message I got while printing with

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"

Upvotes: 3

Views: 9788

Answers (3)

Carlos Colque
Carlos Colque

Reputation: 1

thank you for your answer.. work for me, but you need to add an "aps" in the ios notifications, so .... in this part for "apns" shoud be:

"apns":{
    "payload":{
           "aps":{
             "sound":"default"
                 }
              }
       }

Upvotes: 0

Pratik Butani
Pratik Butani

Reputation: 62429

As per my comment, You have to use your JSON like following way.

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.

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"
            }
        }
    }
}

Reference Link is: Unable to add sound to notification payload

Thank you.

Upvotes: 6

Rasulbek Abdurasulov
Rasulbek Abdurasulov

Reputation: 111

Try this:

{
    "message":{
        "token":"your-token-value",
        "notification":{
                "title":"Test",
                "body":"Test message from server"
        },
        "android":{
            "notification":{
                "sound":"default"
            }
        },
        "apns":{
            "payload":{
                "sound":"default"
            }
        }
    }
}

Upvotes: 0

Related Questions