You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C# FCM推送通知中设置图标及iOS/Android提示音?

FCM通知图标与提示音配置说明

图标和提示音的配置需要结合服务端代码和客户端应用设置共同完成,分平台的具体操作如下:

一、通知图标配置

Android端

  1. 客户端资源准备:把透明背景的PNG图标(推荐命名为ic_notification.png)放入Android项目的res/drawable或res/mipmap目录,需适配不同屏幕尺寸。
  2. 配置方式二选一:
    • 全局默认图标:在AndroidManifest.xml的<application>标签内添加:
      <meta-data
          android:name="com.google.firebase.messaging.default_notification_icon"
          android:resource="@drawable/ic_notification" />
      
      所有未单独指定图标的FCM通知都会使用这个默认图标。
    • 服务端指定单通知图标:在Message对象中添加Android配置段:
      Android = new AndroidConfig()
      {
          Notification = new AndroidNotification()
          {
              Icon = "ic_notification" // 仅填文件名,无需后缀
          }
      }
      

iOS端

iOS通知图标默认使用应用的启动图标,无需服务端额外配置。若要自定义通知图标,需在Xcode中配置Notification Content Extension实现自定义UI,这部分属于客户端开发范畴,服务端无法直接控制。

二、提示音配置

Android端

  1. 客户端资源准备:将自定义提示音文件(如notification_sound.mp3)放入Android项目的res/raw目录。
  2. 配置方式二选一:
    • 全局默认提示音:在AndroidManifest.xml的<application>标签内添加:
      <meta-data
          android:name="com.google.firebase.messaging.default_notification_sound"
          android:resource="@raw/notification_sound" />
      
    • 服务端指定单通知提示音:在AndroidConfig中设置:
      Android = new AndroidConfig()
      {
          Notification = new AndroidNotification()
          {
              Sound = "notification_sound" // 仅填文件名,无需后缀;用系统默认则填"default"
          }
      }
      

iOS端

  1. 客户端资源与配置:将符合要求的提示音文件(如notification_sound.wav,需为Apple支持格式且时长≤30秒)添加到iOS项目,然后在Info.plist中声明:
    <key>UILocalNotificationDefaultSoundName</key>
    <string>notification_sound.wav</string>
    
  2. 服务端指定提示音:在Message对象中添加Apns配置段:
    Apns = new ApnsConfig()
    {
        Aps = new Aps()
        {
            Sound = "notification_sound.wav" // 填完整文件名(带后缀);用系统默认则填"default"
        }
    }
    

修改后的完整示例代码

FirebaseApp.Create(new AppOptions()
{
    Credential = GoogleCredential.FromFile("private_key.json")
});

var registrationToken = "TOKEN_HERE";

var message = new Message()
{
    Data = new Dictionary<string, string>()
    {
        { "myData", "1337" },
    },
    Token = registrationToken,
    Notification = new Notification()
    {
        Title = "Test from code",
        Body = "Here is your test!"
    },
    // Android端图标与提示音配置
    Android = new AndroidConfig()
    {
        Notification = new AndroidNotification()
        {
            Icon = "ic_notification",
            Sound = "notification_sound"
        }
    },
    // iOS端提示音配置
    Apns = new ApnsConfig()
    {
        Aps = new Aps()
        {
            Sound = "notification_sound.wav"
        }
    }
};

string response = FirebaseMessaging.DefaultInstance.SendAsync(message).Result;
Console.WriteLine("Successfully sent message: " + response);

内容的提问来源于stack exchange,提问作者sly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 03:23:27