Android通知声音不起作用

时间:2016-09-26 20:28:02

标签: android xamarin notifications xamarin.android

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());

我哪里错了?为什么我的通知没有任何声音或振动?

2 个答案:

答案 0 :(得分:4)

删除.SetDefaults(NotificationDefaults.All),您重置刚刚构建的通知设置:

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);

答案 1 :(得分:0)

尝试将uri传递给notificationBuilder中的setSound,就像这样

Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); notificationBuilder.setSound(uri);