【问题标题】:Android + Xamarin push notification custom sound will not playAndroid + Xamarin 推送通知自定义声音不会播放
【发布时间】:2016-08-23 17:12:56
【问题描述】:

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

我在 Resources 下创建了一个“raw”文件夹,并添加了一个适当的声音文件。我已确保该文件正在构建为 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);

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

谢谢!!!

【问题讨论】:

  • 这段代码是否在派生自GcmListenerService的类中?

标签: android xamarin notifications push


【解决方案1】:

看起来您很接近,您的代码与我们在多个 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());

【讨论】:

  • 感谢您的想法!我使用了你的建议,不幸的是仍然没有运气......我真的没有想法......
  • 更新:在尝试使用默认铃声后,它可以工作,所以它必须是我引用声音的方式。只是不确定我做错了什么......路径似乎很基本,我已经一遍又一遍地检查它。
  • @JamesScott 你试过没有指定文件类型的声音文件路径吗?
  • 是的,我已经用 /raw/resourcename、/resourceid、/resourcename.wav 试过了……它们都不起作用。有趣的是,当我创建一个 MediaPlayer 对象并将其指向原始资源时,它播放得很好……另一件奇怪的事情是,当我调用 NotificationManager.Notify 时,它只会播放我默认的三星通知音
  • 嗯,我想我终于想通了。如果您的声音有问题,我建议:1)像我一样创建一个媒体播放器对象,看看它是否可以播放您的 Uri 2)检查以确保您为 Notification 对象设置了正确的默认值。就我而言,我将 Defaults 设置为 0 并播放声音...
【解决方案2】:

嗯,我想我终于想通了。如果您的声音有问题,我建议:1)像我一样创建一个媒体播放器对象,看看它是否可以播放您的 Uri 2)检查以确保您为 Notification 对象设置了正确的默认值。就我而言,我将 Defaults 设置为 0 并播放声音...

【讨论】:

  • 面临与您相同的问题。你能分享你的代码吗?
  • @Joker_37 直到现在我才看到你的评论。对不起!如果您仍然想要代码,请告诉我,我会发布我所拥有的。
猜你喜欢
  • 1970-01-01
  • 2017-01-16
  • 2022-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多