Stas Petrov Stas Petrov - 2 months ago 11
Android Question

Android notification sound doesn't work

var notificationBuilder = new Notification.Builder(context)
.SetSmallIcon(Resource.Drawable.Icon)
.SetLargeIcon(BitmapFactory.DecodeResource(Application.Context.Resources, Resource.Drawable.Icon))
.SetContentTitle(title)
.SetContentText(message)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent)
.SetStyle(new Notification.BigTextStyle().BigText(message))
.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
.SetVibrate(new long[] { 1000, 1000 })
.SetDefaults(NotificationDefaults.All);

var notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
notificationManager.Notify(0, notificationBuilder.Build());


Where i was wrong? Why my notifications doesn't have any sound or vibration?

Answer

Remove .SetDefaults(NotificationDefaults.All), you are resetting the notification settings that you just built:

var notification = new Notification.Builder(Application.Context)
    .SetSmallIcon(Resource.Mipmap.Icon)
    .SetLargeIcon(BitmapFactory.DecodeResource(Application.Context.Resources, Resource.Mipmap.Icon))
    .SetContentTitle("Stack")
    .SetContentText("OverFlow")
    .SetAutoCancel(true)
    //.SetContentIntent(pendingIntent)
    .SetStyle(new Notification.BigTextStyle().BigText("OverFlow"))
    .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
    .SetVibrate(new long[] { 1000, 1000 })
    .Build();

var notificationManager = (NotificationManager)Application.Context.GetSystemService(Context.NotificationService);
notificationManager.Notify(1, notification);
Comments