0

I am using AppCenter Push Notification in xamarin android project. Notification received successfully but there is no sound.Kindly Guide me what i should do for notification sound.

protected override void OnStart()
    {
        AppCenter.Start("c71c0b73-a242-469e-ad97-990ca0853f9b", typeof(Push));
        CustomProperties properties = new CustomProperties();
        properties.Set("Sound", true);
        AppCenter.SetCustomProperties(properties);
    }
hammad
  • 61
  • 2

1 Answers1

0

Adding sound for Notification, you can use NotificationChannel.SetSound(),add the mp3 file in your Android project under Resources/raw/soundFile.mp3 and set the build as Android Resource)

private void createNotificationChannel()
    {          
            var channelName = GetString(Resource.String.noti_chan_urgent);
            var channelDescription = GetString(Resource.String.noti_chan_urgent_description);

            // set the vibration patterm for the channel
            long[] vibrationPattern = { 100, 200, 300, 400, 500, 400, 300, 200, 400 };

            // Creating an Audio Attribute
            var alarmAttributes = new AudioAttributes.Builder()
                .SetContentType(AudioContentType.Sonification)
                .SetUsage(AudioUsageKind.Notification).Build();

                // Create the uri for the alarm file                 
            Android.Net.Uri alarmUri = Android.Net.Uri.Parse(${ContentResolver.SchemeAndroidResource}://{Context.PackageName}/{Resource.Raw.soundFile}");


            // create chan1  which is the urgent notifications channel
            var chan1 = new NotificationChannel(PRIMARY_CHANNEL_ID, channelName, NotificationImportance.High)
            {
                Description = channelDescription
            };


            // set the channel properties
            chan1.EnableLights(true);
            chan1.LightColor = Color.Red;
            chan1.SetSound(alarmUri, alarmAttributes);
            chan1.EnableVibration(true);
            chan1.SetVibrationPattern(vibrationPattern);               
            chan1.SetBypassDnd(true);
            chan1.LockscreenVisibility = NotificationVisibility.Public;

            // finally create the notification channel
            var manager = (NotificationManager)GetSystemService(NotificationService);
            manager.CreateNotificationChannel(chan1);
    }
Cherry Bu - MSFT
  • 10,160
  • 1
  • 10
  • 16
  • 1
    I want to send push notification to the users whenever I want, so how I can achieve that? – hammad May 12 '19 at 06:00