0

In my unity project I want play custom sound when I get firebase cloud message, not system default sound.
So after I followed other answers my message looks like,

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

and I placed custom_sound.wav in Asset/Plugins/Android/res/raw. When I unzip my .apk, I can find my sound file is in right location.

But it keeps playing system default sound. Even after I remove sound field. Is there any other thing should I check?

JokyDandy
  • 59
  • 1
  • 8

1 Answers1

0

First: a quick tip when debugging. If you select "Export Project", you can open the generated Gradle project with Android Studio: Build Settings window open with "Export Project" selected Occasionally you have to update the gradle wrapper, but it helps a ton debug things like "is my sound file in res/raw" without having to decompress your APK and poke around.

I think that the issue you're running into now is that sounds are now associated with NotificationChannels (as of Android O) rather than individual notifications, as noted by this StackOverflow post expressing a similar issue. Since this isn't exposed via the Unity SDK.

Fortunately, you can add a channel with Unity.Notifications.Android.

It should be as simple as creating a new public AndroidNotificationChannel(string id, string title, string description, Importance importance) with your id set to "2" (to match your sample notification above. Since this is a string, I would recommend giving this a better name :D).

Then you can call RegisterNotificationChannel with that channel you create as your parameter.

For example, to get your notification above to work, I believe you can write:

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

Let me know if this helps!

--Patrick

Patrick Martin
  • 2,993
  • 1
  • 13
  • 11
  • Thanks for the answer and tip! But sadly, my app still playing default sound. Other changes I made via `AndroidNotificationChannel` works(like importance), but there is no sound field in `AndroidNotificationChannel`. So it seems that Unity built-in notification doesn't support custom sound right now, right? – JokyDandy Dec 19 '19 at 09:42