Amm*_*riq 7 java android firebase-cloud-messaging
我正在从 Firebase 向我的 Android 应用程序发送推送通知,但它仅在收到通知时播放默认声音。
\n\n我已在 fcm 通知对象中设置了自定义声音参数{\xe2\x80\x9csound\xe2\x80\x9d:\xe2\x80\x9dnotificationsound.mp3\xe2\x80\x9d},并且该文件根据(https://firebase.google.com/docs/cloud-messaging/http-server-ref)存在于 res/raw 文件夹中\n但是它\xe2\x80\x99s 仍在所有应用程序状态(后台、前台和终止)上播放默认声音。\n这是我在 Android 上发送通知的请求正文:
{\n"to" : \xe2\x80\x9csome id\xe2\x80\x9d,\n"notification": {\n"title":"asdasd",\n"body":"sdsad",\n"click_action" : "MAIN",\n"sound":"notificationsound.mp3"\n},\n"data": {\n"title" : "Something",\n"body" : "Important",\n"type" : "message"\n},\n"priority": "high"\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我可以做什么来播放自定义通知声音。
\n小智 12
我也在寻找 android 中 firebase 通知的自定义声音的解决方案,并且我已经通过通知通道解决了这个问题。
我创建了一个带有自定义声音的通知通道,该声音在应用程序后台状态下收到通知后播放。
您可以参考以下通知渠道链接。
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); // If you are writting code in fragment
Run Code Online (Sandbox Code Playgroud)
或者
NotificationManager notificationManager = (NotificationManager) getSystemService(NotificationManager.class); // If you are writting code in Activity
Run Code Online (Sandbox Code Playgroud)
创建通知通道函数
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();
Run Code Online (Sandbox Code Playgroud)
要实现此目的,您需要在 firebase 通知请求对象中传递android_channel_id属性。
{
"notification": {
"body": "this is testing notification",
"title": "My App",
"android_channel_id": "cnid"
},
"to": "token"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21733 次 |
| 最近记录: |