Xamarin.Android get resource id without ResourceManager.

This content has 4 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

If calling ResourceManager.GetResource(“resourceName”) doesn’t work because the framework doesn’t return the resource you want, you can bypass it with the following code snippet:

            string audioFileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(audiofile);
            int audioResourceId = (int)typeof(Resource.Raw).GetField(audioFileNameWithoutExtension).GetValue(null);
            Android.Net.Uri audioUri = Android.Net.Uri.Parse(ContentResolver.SchemeAndroidResource + "://" + Application.Context.PackageName + "/" + audioResourceId);

            channel.SetSound(audioUri, audioAttributes);

I used this code snippet to set a custom push notification sound

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.