英文:
Custom sound in Firebase push notification
问题
我从Firebase向我的Android应用发送推送通知,但只有在接收通知时播放默认提示音。
我在FCM通知对象中设置了自定义提示音参数{“sound”:”notificationsound.mp3”}
,并且根据(https://firebase.google.com/docs/cloud-messaging/http-server-ref)中的说明,文件位于res/raw文件夹中。
但是它仍然在所有应用程序状态(后台、前台和关闭)下播放默认提示音。
这是我在Android上发送通知的请求正文:
{
"to" : "some id",
"notification": {
"title":"asdasd",
"body":"sdsad",
"click_action" : "MAIN",
"sound":"notificationsound.mp3"
},
"data": {
"title" : "Something",
"body" : "Important",
"type" : "message"
},
"priority": "high"
}
我该怎么做才能播放自定义通知音呢?
英文:
I am sending push notification from Firebase to my Android application, but it is only playing the default sound when the notification is recieved.
I have set the custom sound param {“sound”:”notificationsound.mp3”}
in the fcm notification object and the file is present in the res/raw folder according to (https://firebase.google.com/docs/cloud-messaging/http-server-ref)
But It’s still playing the default sound on all app states (background, foreground and killed).
This is my request body for sending the notification on Android :
{
"to" : “some id”,
"notification": {
"title":"asdasd",
"body":"sdsad",
"click_action" : "MAIN",
"sound":"notificationsound.mp3"
},
"data": {
"title" : "Something",
"body" : "Important",
"type" : "message"
},
"priority": "high"
}
What I can do to play the custom notification sound.
答案1
得分: 10
我还在寻找在 Android 中为 Firebase 通知设置自定义声音的解决方案,我已经通过通知渠道(Notification Channel)解决了这个问题。
我创建了一个带有自定义声音的通知渠道,该声音在应用程序处于后台状态接收通知后播放。
你可以参考以下通知渠道的链接。
https://medium.com/exploring-android/exploring-android-o-notification-channels-94cd274f604c
https://developer.android.com/training/notify-user/channels
你需要将你的 mp3 文件放在 /res/raw/ 路径下。
请查看代码。
NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(NotificationManager.class); // 如果你在 Fragment 中编写代码
或者
NotificationManager notificationManager = (NotificationManager) getSystemService(NotificationManager.class); // 如果你在 Activity 中编写代码
createNotificationChannel 函数
private void createNotificationChannel() {
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.sample); // 这里的 FILE_NAME 是你想要播放的文件的名称
// 仅在 API 26+ 上才创建 NotificationChannel,因为 NotificationChannel 类是新的,不在支持库中
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "mychannel";
String description = "testing";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
NotificationChannel channel = new NotificationChannel("cnid", name, importance);
channel.setDescription(description);
channel.enableLights(true); channel.enableVibration(true);
channel.setSound(sound, audioAttributes);
notificationManager.createNotificationChannel(channel);
}
};
createNotificationChannel();
要实现这一点,你需要在 Firebase 通知请求对象中传递 android_channel_id 属性。
{
"notification": {
"body": "this is testing notification",
"title": "My App",
"android_channel_id": "cnid"
},
"to": "token"
}
英文:
I was also looking for the solution to custom sound for firebase notification in the android, And I have solved this problem through Notification Channel.
I have created one notification channel with custom sound, that sound plays after receiving notification in the application background state.
You can refer following links of the notification channel.
https://medium.com/exploring-android/exploring-android-o-notification-channels-94cd274f604c
https://developer.android.com/training/notify-user/channels
You need to put your mp3 file at /res/raw/ path.
Please find the code.
NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(NotificationManager.class); // If you are writting code in fragment
OR
NotificationManager notificationManager = (NotificationManager) getSystemService(NotificationManager.class); // If you are writting code in Activity
createNotificationChannel function
private void createNotificationChannel() {
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.sample); //Here is FILE_NAME is the name of file that you want to play
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library if
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
CharSequence name = "mychannel";
String description = "testing";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
NotificationChannel channel = new NotificationChannel("cnid", name, importance);
channel.setDescription(description);
channel.enableLights(true); channel.enableVibration(true);
channel.setSound(sound, audioAttributes);
notificationManager.createNotificationChannel(channel);
}
};
createNotificationChannel();
To achieve this you need to pass android_channel_id property in the firebase notification request object.
{
"notification": {
"body": "this is testing notification",
"title": "My App",
"android_channel_id": "cnid"
},
"to": "token"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论