Skip to content

Push type 3 doesn't play sound on Android #3116

@jsfan3

Description

@jsfan3

Sending a push notification of type 3, as described in the developer guide, causes that Android 10 doesn't play the default sound notification, regardless if the app is open, in background or closed.

I didn't test previous versions of Android. This issue doesn't affect iOS, that plays the default sound notification.

On Stack Overflow, there is a solution: https://stackoverflow.com/a/37959847
However, this solution is not applicable to my Spring Boot code, I suppose that the fix should be done on your Push servers.

This is my code, note that I added "&sound=default" +, but it doesn't work: this parameter is simply ignored.

    public void sendPushNotification(String deviceId, String messageBody, int type) throws IOException {
        HttpURLConnection connection = (HttpURLConnection)new URL("https://push.codenameone.com/push/push").openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        String cert = ITUNES_DEVELOPMENT_PUSH_CERT;
        String pass = ITUNES_DEVELOPMENT_PUSH_CERT_PASSWORD;
        if(ITUNES_PRODUCTION_PUSH) {
            cert = ITUNES_PRODUCTION_PUSH_CERT;
            pass = ITUNES_PRODUCTION_PUSH_CERT_PASSWORD;
        }
        String query = "token="  + PUSH_TOKEN +
            "&device=" + URLEncoder.encode(deviceId, "UTF-8") +
            "&type=" + type +
            "&auth=" + URLEncoder.encode(FCM_SERVER_API_KEY, "UTF-8") +
            "&certPassword=" + URLEncoder.encode(pass, "UTF-8") +
            "&cert=" + URLEncoder.encode(cert, "UTF-8") +
            "&body=" + URLEncoder.encode(messageBody, "UTF-8") +
            "&sound=default" +
            "&production=" + ITUNES_PRODUCTION_PUSH;
        try (OutputStream output = connection.getOutputStream()) {
            output.write(query.getBytes("UTF-8"));
        }
        int c = connection.getResponseCode();
        if(c == 200) {
            try (InputStream i = connection.getInputStream()) {
                byte[] data = utilities.readInputStream(i);
                log.info("Push server returned JSON: " + new String(data));
            }
        } else {
            log.error("Error response code from push server: " + c);
        }
    }

I call that method from a Spring Boot controller in a way similar to:

notificationServices.sendPushNotification(uniqueId, "Visible message";Hidden message", 3);

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions