繁体   English   中英

Android + Xamarin推送通知自定义声音将无法播放

[英]Android + Xamarin push notification custom sound will not play

我一直在使用在Android Xamarin应用程序中播放自定义声音的简单概念,但是无论我尝试哪种声音都不会播放。

我在资源下创建了一个“原始”文件夹,并添加了一个适当的声音文件。 我已确保该文件被构建为Android资源。

这是我的代码:

// note I have also tried just /raw/NotificationSound, /integeridofresource and many other formats
string sSoundUrl = "android.resource://" + AppGlobals.PackageName + "/raw/NotificationSound.wav";

Notification nNotification = new Notification.Builder(this.Activity).SetSmallIcon(Resource.Drawable.AppIcon)
                .SetContentTitle("Test title")
                .SetContentText("Hello notifications")
                .SetAutoCancel(true)
                .SetSound(Android.Net.Uri.Parse(sSoundUrl)).Build();

// I have tried 0, All, NotificationDefaults.Sound basically all different  combinations
nNotification.Defaults = NotificationDefaults.Lights;

NotificationManager nmNotManager = (NotificationManager)this.Activity.GetSystemService(Context.NotificationService);

nmNotManager.Notify(0, nNotification);

我希望有人能发现我做错了什么...

谢谢!!!

您似乎很亲密,您的代码与我们在多个Xamarin.Android应用程序中所做的工作之间只有一些微妙的区别。

我相信明显的区别在于,我们使用GcmListenerService派生的类作为上下文,而不是this.Activity ,我们忽略了路径中的“ .wav”扩展名,为通知生成了一个唯一的id,并设置了意图。

这是一些显示该方法的代码:

        var intent = new Intent(context, typeof(MainActivity));
        intent.PutExtra(MainActivity.GoToAction, action);
        intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
        var pushId = DateTime.Now.TimeOfDay.Milliseconds;
        var pendingIntent = PendingIntent.GetActivity(context, pushId, intent, PendingIntentFlags.OneShot);

        // Set custom push notification sound.
        var pathToPushSound = "android.resource://" + context.ApplicationContext.PackageName + "/raw/pushalert";
        var soundUri = Android.Net.Uri.Parse(pathToPushSound);

        var notificationBuilder = new Android.App.Notification.Builder(context)
            .SetSmallIcon(Resource.Drawable.icon_transparent)
            .SetContentTitle(title)
            .SetContentText(message)
            .SetAutoCancel(true)
            .SetSound(soundUri)
            .SetStyle(new Android.App.Notification.BigTextStyle().BigText(message))
            .SetVibrate(new long[] {100, 1000, 100})
            .SetLights(Android.Resource.Color.HoloOrangeDark, 1, 1)
            .SetContentIntent(pendingIntent);

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

好吧,我想我终于明白了。 如果您在声音方面遇到问题,我建议:1)像我一样创建一个Media Player对象,看看它是否可以播放Uri 2)检查以确保为Notification对象设置了正确的默认值。 就我而言,我将“默认值”设置为0并播放了声音...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM