首页 > 解决方案 > 带有 Xamrin Forms 的 Firebase 自定义声音通知适用于 IOS,但在应用未运行时不适用于 android

问题描述

我到处搜索并尝试了许多没有运气的解决方案。我正在使用 API 向我的应用程序(Android / IOS)发送通知,其中包含已包含在我的应用程序文件中的自定义声音文件。即使应用程序关闭,声音也适用于 IOS,但在应用程序在后台运行之前,它不适用于 Android。这是我使用的 JSON

{
  to = deviceId,
  priority = "high",
  content_available = true,
  notification = new
   {
   body = englishMessage,
   title = title,
   badge = 1,
   content_available = true
   },
   android = new
    {
    ttl="86400s",
    priority= "high",
    notification = new
     {
      sound = SoundFileName,
     },
    },
     apns = new
     {
      payload = new
      {
       aps = new
       {
        sound = SoundFileName,
        content_available = true,
        badge = 1,
       },
      },
      customKey = "test app",
     }
  }

标签: android.netfirebasexamarinxamarin.forms

解决方案


当您的应用程序处于前台时,通知是由您手动生成的,这意味着您需要手动将此声音添加到通知中,所以您所做的就像

var soundUri = Android.Net.Uri.Parse(pathToPushSound);

然后在通知管理器中添加如下方法

     var notiManager= Notification.Builder(context)
                                  .SetSound(soundUri) ...

推荐阅读