统一的自定义 FCM 通知声音 android

Custom FCM notification sound for unity android

在我的 unity 项目中,我想在收到 firebase 云消息时播放自定义声音,而不是系统默认声音。
所以在我遵循其他答案之后,我的消息看起来像,

{
    "to": "some_key",
    "notification": {
        "title": "Title",
        "android_channel_id": "2",
        "body": "Body",
        "sound": "custom_sound.wav"
    }
}

我把 custom_sound.wav 放在 Asset/Plugins/Android/res/raw 中。当我解压缩我的 .apk 时,我可以发现我的声音文件位于正确的位置。

但是一直播放系统默认声音。即使在我移除声场之后。我还应该检查其他什么吗?

首先:调试时的快速提示。如果你 select "Export Project",你可以用 Android Studio 打开生成的 Gradle 项目: 有时您必须更新 gradle 包装器,但它有助于大量调试 "is my sound file in res/raw" 之类的东西,而无需解压缩您的 APK 并四处寻找。

我认为您 运行 现在遇到的问题是声音现在与 NotificationChannels(从 Android O 开始)相关联,而不是单独的通知,如 [=16= 所述]表达了类似的问题。因为这不是通过 Unity SDK 公开的。

幸运的是,您可以使用 Unity.Notifications.Android 添加频道。

它应该像创建一个新的一样简单 public AndroidNotificationChannel(string id, string title, string description, Importance importance)id 设置为 "2"(以匹配上面的示例通知。由于这是一个字符串,我建议给它一个更好的名称 :D)。

然后您可以使用您创建的频道作为参数调用 RegisterNotificationChannel

例如,要让上面的通知生效,我相信你可以这样写:

var notificationChannel = new NotificationChannel("2", "Channel 2 (working title)", "This is the 2nd channel", Importance.Default);
AndroidNotificationCenter.RegisterNotificationChannel(notificationChannel);

如果有帮助请告诉我!

--帕特里克