【问题标题】:Custom FCM notification sound for unity androidunity android 的自定义 FCM 通知声音
【发布时间】:2020-04-10 21:21:39
【问题描述】:

在我的统一项目中,我想在收到 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 时,我可以发现我的声音文件位于正确的位置。

但它一直播放系统默认声音。即使在我移除声场之后。我还有什么需要检查的吗?

【问题讨论】:

    标签: firebase unity3d firebase-cloud-messaging


    【解决方案1】:

    首先:调试时的快速提示。如果选择“Export Project”,可以用Android Studio打开生成的Gradle项目: 有时您必须更新 gradle 包装器,但它有助于大量调试诸如“我的声音文件在 res/raw 中”之类的东西,而无需解压缩您的 APK 并四处寻找。

    我认为您现在遇到的问题是声音现在与 NotificationChannels(从 Android O 开始)相关联,而不是与单个通知相关联,正如 this StackOverflow post 所指出的那样,表达了类似的问题。因为这不是通过 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);
    

    如果这有帮助,请告诉我!

    --帕特里克

    【讨论】:

    • 感谢您的回答和提示!但可悲的是,我的应用程序仍在播放默认声音。我通过AndroidNotificationChannel 所做的其他更改(如重要性),但AndroidNotificationChannel 中没有声场。所以现在看来​​ Unity 内置通知不支持自定义声音,对吧?
    猜你喜欢
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 2022-07-12
    • 1970-01-01
    • 2023-03-28
    • 2021-11-15
    相关资源
    最近更新 更多