简体   繁体   English

Firebase 推送通知中的自定义声音

[英]Custom sound in Firebase push notification

I am sending push notification from Firebase to my Android application, but it is only playing the default sound when the notification is recieved.我正在从 Firebase 向我的 Android 应用程序发送推送通知,但它只在收到通知时播放默认声音。

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).我已经在 fcm 通知 object 中设置了自定义声音参数{“sound”:”notificationsound.mp3”} ,并且文件存在于 res/raw 文件夹中,根据( https://firebase.google.com/docs/cloud -messaging/http-server-ref )但它仍在所有应用程序状态(背景、前景和已终止)上播放默认声音。 This is my request body for sending the notification on Android:这是我在 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.我可以做些什么来播放自定义通知声音。

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.我还在 android 中寻找 firebase 通知的自定义声音的解决方案,我已经通过通知通道解决了这个问题。

I have created one notification channel with custom sound, that sound plays after receiving notification in the application background state.我创建了一个带有自定义声音的通知通道,该声音在应用程序后台 state 中收到通知后播放。

You can refer following links of the notification channel.您可以参考以下通知渠道的链接。

https://medium.com/exploring-android/exploring-android-o-notification-channels-94cd274f604c https://medium.com/exploring-android/exploring-android-o-notification-channels-94cd274f604c

https://developer.android.com/training/notify-user/channels https://developer.android.com/training/notify-user/channels

You need to put your mp3 file at /res/raw/ path.您需要将您的 mp3 文件放在/res/raw/路径中。

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 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.为此,您需要在 firebase 通知请求 object 中传递android_channel_id属性。

{
 "notification": {
 "body": "this is testing notification",
 "title": "My App",
 "android_channel_id": "cnid"
 },
 "to": "token"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM