[Huawei Developers] muraliameakula:
Background

Given the sheer number of apps out there, it's important to make your own app stand out from the crowd. Custom ringtones are a good way to do that, for example, if you've developed a payment, online education, or video app. When a tone is played to indicate a message has been delivered, users will be able to identify your app in an instant, and develop a greater appreciation for it.

So, let's move on to the process for creating custom ringtones in HUAWEI Push Kit to increase your message impressions.

Basic Ideas

Procedure

1. Set a ringtone for the service and communication messaging channel.

Restrictions: Make sure that the EMUI version is 9.1.0 or later and the Push Service app version is 9.1.1 or later.

To view the Push Service app version, go to Settings > Apps > Apps on your device, and search for Push Service.

​

1) Perform configuration on your app.

a. The ringtone to be used can only be stored in the /res/raw directory of the app.

b. Supported ringtone file formats are: MP3, WAV, and MPEG.

For example, store the bell.mp3 file in /res/raw.

View attachment 5362425

2) Perform configuration on your app server.

a. Construct a common downlink message request. In the request:

b. Set importance to NORMAL, indicating that the message is a service and communication message.

c. Set default_sound to false, indicating that the value of sound is used.

d. Set sound to the path where the custom ringtone is stored on the app.

For example, for the bell.mp3 file on the app, set sound to /raw/bell.

{
"validate_only": false,
"message": {
"android": {
"notification": {
"importance": "NORMAL",
"title": "Test ringtone",
"body": "Ringtone bell for this message",
"click_action": {
"type": 3
},
"default_sound": false,
"sound": "/raw/bell"
}
},
"token": [
"xxx"
]
}
}

3) Effects

4) FAQs

a. Q: Why can I only set the ringtone for the service and communication messaging channel?

A: For the other channel, that is, news and marketing messaging channel, the default message reminder mode is no lock screen, no ringtone, and no vibration. Therefore, the ringtone will not take effect even if it is set. For news and marketing messages, the user will need to set a ringtone.

b. Q: Why do I need to set the default ringtone before sending a message for the first time after the app is installed?

A: The ringtone is an attribute for the messaging channel. Therefore, the ringtone will only take effect after being set during the channel creation. Once the channel is created, the user will need to manually modify the messaging settings for a channel.

2. Set a ringtone for a custom messaging channel.

Restrictions: Make sure that the EMUI version is 10.0.0 or later and the Push Service app version is 10.0.0 or later.

1) Perform configuration on your app.

a. Save the ringtone file to the /assets or /res/raw directory.

For example, store the bell.mp3 file in /res/raw.

b. Create a messaging channel. (Note: The custom ringtone can only be set when the channel level is NotificationManager.IMPORTANCE_DEFAULT or higher.)

c. Set the ringtone.

For example, create the messaging channel "test" and set the channel ringtone to "/res/raw/bell.mp3".

createNotificationChannel("test", "Channel 1", NotificationManager.IMPORTANCE_DEFAULT);

private String createNotificationChannel(String channelID, String channelNAME, int level) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
channel.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bell), Notification.AUDIO_ATTRIBUTES_DEFAULT);
manager.createNotificationChannel(channel);
return channelID;
} else {
return "";
}
}

2) Perform configuration on your app server.

a. Construct a common downlink message request. In the request:

b. Set importance to NORMAL, indicating that the message is a service and communication message.

c. Set channel_id to the ID of the channel created on the app, so that the message can be displayed on the channel.

For example, set channel_id to test.

{
"validate_only": false,
"message": {
"android": {
"notification": {
"importance": "NORMAL",
"title": "Test ringtone",
"body": "Custom ringtone for the message displayed through the channel test",
"click_action": {
"type": 3
},
"channel_id": "test"
}
},
"token": [
"xxx"
]
}
}

3) Effects

4) FAQs

Q: Why do I need to set importance to NORMAL for the custom channel?

A: For the other channel, that is, news and marketing messaging channel, the default message reminder mode is no lock screen, no ringtone, and no vibration, which will minimize the distraction to users.

Precautions

1. The ringtone set by a user has the highest priority. If the user changes it to another ringtone, the new ringtone will prevail.

2. The following table lists the impact of each field in the downlink message on the ringtone (the intelligent classification is not considered).

Channel Status (Whether the Channel Has Been Created)	default_sound	sound	importance

(EMUI 10.0.0 or later)	channel_id	Ringtone
Any	Any value	Any value	LOW	Any value	None
Not created	true	Any value	NORMAL	Empty	Default system ringtone
Not created	false	/raw/bell	NORMAL	Empty	bell.mp3
Not created	false	Empty	NORMAL	Empty	Default system ringtone
Any	Any value	Any value	NORMAL	test	Ringtone for the channel "test"
Created	Any value	Any value	NORMAL	Empty	Ringtone for the original channel, which has not been changed
Created	Any value	Any value	NORMAL	test	Ringtone for the original channel "test", which has not been changed

To learn more, please visit:

HUAWEI Developers official website

Development Guide

Reddit to join developer discussions

GitHub or Gitee to download the demo and sample code

Stack Overflow to solve integration problems

Follow our official account for the latest HMS Core-related news and updates.

Original Source