当前位置: 首页 > 知识库问答 >
问题:

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

卫仲卿
2023-03-14

我正在从Firebase向我的Android应用程序发送推送通知,但它仅在收到通知时播放默认声音。

我在fcm通知对象中设置了自定义声音参数{“sound”:“notificationsound.mp3”},该文件根据(https://firebase.google.com/docs/cloud-messaging/http-server-ref)但它仍然在所有应用程序状态(背景、前景和终止)上播放默认声音。这是我在Android上发送通知的请求主体:

{
"to" : “some id”,
"notification": {
"title":"asdasd",
"body":"sdsad",
"click_action" : "MAIN",
"sound":"notificationsound.mp3"
},
"data": {
"title" : "Something",
"body" : "Important",
"type" : "message"
},
"priority": "high"
}

我可以做什么来播放自定义通知声音。

共有1个答案

郭皓
2023-03-14

我也在寻找android中firebase通知的定制声音解决方案,我已经通过通知通道解决了这个问题。

我创建了一个带有自定义声音的通知通道,该声音在应用程序后台状态下收到通知后播放。

您可以参考通知通道的以下链接。

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

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

您需要将mp3文件放在/res/raw/path。

请查找代码。

NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(NotificationManager.class); // If you are writting code in fragment

NotificationManager notificationManager = (NotificationManager) getSystemService(NotificationManager.class); // If you are writting code in Activity

createNotificationChannel函数

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(); 

要实现这一点,您需要在Firebase通知请求对象中传递android_channel_id属性。

{
 "notification": {
 "body": "this is testing notification",
 "title": "My App",
 "android_channel_id": "cnid"
 },
 "to": "token"
}
 类似资料:
  • 我正在写一个iOS的应用程序,使用laravel的API和谷歌Firebase的推送通知。当我使用Firebase云消息传递发送消息推送时,它会进入我的设备。当我使用laravel发送推送通知时,它不会影响。这里是我的脚本发送推送通知laravel: 它返回一个成功的结果,但通知未发送到iOS设备。 PS:它可以在Android设备上成功运行。

  • 我正在我的应用程序中实施第三部分服务(FreshChat,一种允许您将帮助台集成到应用程序或网站中的服务),除了聊天的推送通知之外,一切都很好。Freshchat通过Firebase工作(Firebase推送通知不会出现问题),但Fresat通知不起作用。我遵循了newchat的官方留档,但有已弃用的方法,绝对不起作用(我尝试了很多) 这是Firebase云消息传递代码: 以及FreshChat文

  • 这是舱单 这是我的注册令牌类 这是我的Firebase服务类

  • 我正在从调用firebase api的应用服务器向用户发送数据消息。用户正在接收通知,但通知到达时不会播放声音。 这是代码 有什么问题吗???

  • 我将发送带有自定义单击操作的推送通知,以设置通知的以在应用程序处于前台或后台时在我的项目中打开一个确切的。在所有的帖子和评论中都写到不可能从控制台发送带有自定义点击动作的通知,但在控制台中的“高级选项”中,可以设置自定义数据键和值。 为什么不能设置键及其值? 如果可能的话,告诉我该怎么做。我的意思是确切的意图过滤代码是什么?我应该将什么设置为值? 假设我想通过单击Notification打开。

  • 首先,我想声明我一直在研究推送通知和web通知之间的关系,但我有点困惑。 我从这里读到PWAs的推送通知在Safari上的iOS(iPhone)不起作用:从PWA向iOS发送推送通知 然而,如果iPhone用户使用的是Chrome,这是否意味着它们可以工作呢?或者推送通知在任何浏览器上对iPhone中的PWAs都不起作用? 这就把我带到了web通知。web通知在后台对PWAs起作用吗?我的问题是w