---
# System prepended metadata

title: Firebase Messaging Topic for C#
tags: [Firebase, C#, Messaging]

---

Firebase Messaging Topic for C#
===

## 文章內容

[TOC]

## 主題(Topic)說明

關於主題需要記住的一些事項：

1. 主題消息最適合天氣等內容或其他公開可用的信息。
2. 主題消息針對吞吐量而不是延遲進行了優化。為了快速、安全地傳遞到單個設備或小組設備，請將消息定位到註冊令牌，而不是主題。
3. 如果您需要將消息發送到每個用戶的多個設備，請考慮針對這些用例進行設備群組消息傳遞。
4. 主題消息支持每個主題的無限訂閱。但是，FCM 在以下領域實施限制：
5. 一個應用實例最多可以訂閱 2000 個主題。
6. 如果您使用批量導入訂閱應用實例，則每個請求限制為 1000 個應用實例。
7. 新訂閱的頻率受每個項目的速率限制。如果您在短時間內發送太多訂閱請求，FCM 服務器將響應429 RESOURCE_EXHAUSTED （“超出配額”）響應。使用指數退避重試。is, start here!


管理服務器上的主題訂閱 C#
---
> 您可以將註冊令牌列表傳遞給 Firebase Admin SDK 訂閱方法，以便為相應的設備**訂閱主題**：[FirebaseMessagingSnippets.cs ](https://github.com/firebase/firebase-admin-dotnet/blob/2d0a1a5a1ba6e219f0418c9e8f7265b887aa8563/FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseMessagingSnippets.cs#L327-L341)
```csharp=
// These registration tokens come from the client FCM SDKs.
var registrationTokens = new List<string>()
{
    "YOUR_REGISTRATION_TOKEN_1",
    // ...
    "YOUR_REGISTRATION_TOKEN_n",
};

// Subscribe the devices corresponding to the registration tokens to the
// topic
var response = await FirebaseMessaging.DefaultInstance.SubscribeToTopicAsync(
    registrationTokens, topic);
// See the TopicManagementResponse reference documentation
// for the contents of response.
Console.WriteLine($"{response.SuccessCount} tokens were subscribed successfully");
```

> Admin FCM API 還允許您通過將註冊令牌傳遞給適當的方法來**取消訂閱**設備的主題：[FirebaseMessagingSnippets.cs ](https://github.com/firebase/firebase-admin-dotnet/blob/2d0a1a5a1ba6e219f0418c9e8f7265b887aa8563/FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseMessagingSnippets.cs#L327-L341)
```csharp=
// These registration tokens come from the client FCM SDKs.
var registrationTokens = new List<string>()
{
    "YOUR_REGISTRATION_TOKEN_1",
    // ...
    "YOUR_REGISTRATION_TOKEN_n",
};

// Unsubscribe the devices corresponding to the registration tokens from the
// topic
var response = await FirebaseMessaging.DefaultInstance.UnsubscribeFromTopicAsync(
    registrationTokens, topic);
// See the TopicManagementResponse reference documentation
// for the contents of response.
Console.WriteLine($"{response.SuccessCount} tokens were unsubscribed successfully");
```


构建应用服务器发送请求 C#
---
> 向主题发送消息
```csharp=
// The topic name can be optionally prefixed with "/topics/".
var topic = "highScores";

// See documentation on defining a message payload.
var message = new Message()
{
    Data = new Dictionary<string, string>()
    {
        { "score", "850" },
        { "time", "2:45" },
    },
    Topic = topic,
};

// Send a message to the devices subscribed to the provided topic.
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);
```

## 資料來源

:::info
1. [Firebase Topic訂閱、取消訂閱 C#](https://firebase.google.com/docs/cloud-messaging/android/topic-messaging#c)
2. [Firebase 建構服務器發送請求 C#](https://firebase.google.com/docs/cloud-messaging/send-message#c)
:::

###### tags: `Firebase` `Messaging` `C#`
