【问题标题】:specific ringtone firebase notification xamarin.android特定铃声 firebase 通知 xamarin.android
【发布时间】:2020-06-12 14:02:36
【问题描述】:

如何强制推送通知运行铃声而不是默认通知声音 有什么方法可以覆盖 firebase onMessageReceived 的默认行为

【问题讨论】:

    标签: android firebase xamarin.android firebase-cloud-messaging


    【解决方案1】:

    您可以在 firebase 通知中设置特定的铃声。

    当您使用 API 26 或更高版本时,您将使用通知通道。使用SetSound的频道会覆盖默认通知声音。

    使用Channel的SetSound方法设置铃声。

    channel.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Ringtone),alarmAttributes);
    

    创建频道的完整代码:

      void CreateNotificationChannel()
        {
            if (Build.VERSION.SdkInt < BuildVersionCodes.O)
            {
                // Notification channels are new in API 26 (and not a part of the
                // support library). There is no need to create a notification 
                // channel on older versions of Android.
                return;
            }
            var alarmAttributes = new AudioAttributes.Builder()
                  .SetContentType(AudioContentType.Sonification)
                  .SetUsage(AudioUsageKind.Notification).Build();
            var channel = new NotificationChannel(CHANNEL_ID, "FCM Notifications", NotificationImportance.Default)
            {
                Description = "Firebase Cloud Messages appear in this channel"
            };   
         channel.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Ringtone),alarmAttributes);
            var notificationManager = (NotificationManager)GetSystemService(NotificationService);
            notificationManager.CreateNotificationChannel(channel);
        }
    

    您可以从以下链接下载有关 Firebase 通知的代码示例。使用频道的SetSound 进行更改将设置特定的铃声。 https://docs.microsoft.com/en-us/samples/xamarin/monodroid-samples/firebase-fcmnotifications/

    【讨论】:

      猜你喜欢
      • 2022-06-24
      • 1970-01-01
      • 2014-01-20
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多