简体   繁体   English

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

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

I've been working with the simple concept of playing a custom sound in my Android Xamarin app, but no matter what I try the sound will not play. 我一直在使用在Android Xamarin应用程序中播放自定义声音的简单概念,但是无论我尝试哪种声音都不会播放。

I have created a "raw" folder under Resources, and added an appropriate sound file. 我在资源下创建了一个“原始”文件夹,并添加了一个适当的声音文件。 I have made sure that file is being built as an Android resource. 我已确保该文件被构建为Android资源。

Here's my code: 这是我的代码:

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

I'm hoping somebody can spot what I'm doing wrong... 我希望有人能发现我做错了什么...

Thanks!!! 谢谢!!!

Looks like you are close, there are just a few subtle differences between your code and what we have working in several Xamarin.Android apps. 您似乎很亲密,您的代码与我们在多个Xamarin.Android应用程序中所做的工作之间只有一些微妙的区别。

I believe the salient differences are that we use the GcmListenerService -derived class as the context rather than this.Activity , we omit the ".wav" extension in the path, we generate a unique-ish id for the notification, and we set an intent. 我相信明显的区别在于,我们使用GcmListenerService派生的类作为上下文,而不是this.Activity ,我们忽略了路径中的“ .wav”扩展名,为通知生成了一个唯一的id,并设置了意图。

Here is some code that shows the approach: 这是一些显示该方法的代码:

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

Well I think I finally figured it out. 好吧,我想我终于明白了。 If you are having trouble with your sounds I would recommend: 1) Create a Media Player object like I did and see if it can play your Uri 2) Check to make sure you have the right defaults set for the Notification object. 如果您在声音方面遇到问题,我建议:1)像我一样创建一个Media Player对象,看看它是否可以播放Uri 2)检查以确保为Notification对象设置了正确的默认值。 In my case I set Defaults to 0 and it played the sound... 就我而言,我将“默认值”设置为0并播放了声音...

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

相关问题 Android推送通知如何播放默认声音 - Android push notification how to play default sound Phonegap:如何在Android上播放推送通知的声音 - Phonegap: how to play sound on android for a push notification Android Firebase 推送通知自定义声音不起作用 - Android Firebase push notification custom sound is not working Android 推送通知的自定义声音不起作用 (FCM) - Custom sound for Android Push Notification not working (FCM) Android Oreo无法播放自定义声音以进行通知 - Android Oreo Does not Play Custom Sound for Notification 自定义通知声音在 android 中不播放 - Custom notification sound does not play in android 使用自定义声音将GCM推送通知发送到android - Send GCM Push Notification to android with custom sound 如何使用本地通知/ Notification.Builder在Android的Xamarin.Forms中播放自定义声音 - How to play custom sound in Xamarin.Forms on Android using Local Notifications / Notification.Builder IBM Worklight 5.0.5-在推送通知上播放定制声音 - IBM Worklight 5.0.5 - Play custom sound on push notification IBM Worklight 5.0.6-在推送通知错误时播放定制声音 - IBM Worklight 5.0.6 - Play custom sound on push notification error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM