在 C++ 用戶端應用程式中接收訊息
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
如要接收簡單的下游訊息,每個用戶端應用程式都必須在 firebase::messaging::Listener
API 上實作方法。
初始化 FCM
您必須先初始化 FCM,才能使用該物件取得註冊權杖或接收訊息。
如要初始化 FCM,請呼叫 ::firebase::messaging::Initialize
,並提供 ::firebase::App
物件和 ::firebase::messaging::Listener
類別的實作項目。
MyListener my_listener_implementation;
::firebase::messaging::Initialize(app, &my_listener_implementation);
存取註冊權杖
應用程式首次啟動時,FCM SDK 會為用戶端應用程式執行個體產生註冊權杖。如要指定單一裝置,或為 FCM 建立裝置群組,就必須存取這個權杖。
您可以透過 ::firebase::messaging::Listener::OnTokenReceived
虛擬函式存取權杖的值。
void OnTokenReceived(const char* token) {
LogMessage("The registration token is `%s`", token);
// TODO: If necessary send token to application server.
}
接收及處理訊息
如要接收訊息,您的 Listener 類別必須實作
OnMessage
虛擬函式。
覆寫 OnMessage
覆寫 ::firebase::messaging::Listener::OnMessage
方法,即可根據收到的訊息執行動作,並取得訊息資料:
void OnMessage(const ::firebase::messaging::Message& message) {
LogMessage(TAG, "From: %s", message.from.c_str());
LogMessage(TAG, "Message ID: %s", message.message_id.c_str());
}
訊息可以代表不同類型的傳入資料。最常見的情況是,訊息會在開發人員啟動後傳送至應用程式。系統也會將訊息傳送至應用程式,代表訊息傳送事件、訊息傳送錯誤事件和訊息刪除事件。您可以檢查 Message::message_type
欄位,區分這些特殊事件。
已刪除訊息
FCM 伺服器刪除待處理訊息時,會傳送至您的應用程式。
Message::message_type
將為 "deleted_messages"
。訊息可能因下列原因而遭刪除:
FCM 伺服器上儲存的郵件過多。
如果應用程式伺服器在裝置離線時,傳送大量無法摺疊的訊息至 FCM 伺服器,就可能發生這種情況。
裝置已許久未連線,且應用程式伺服器最近 (過去 4 週內) 曾傳送訊息給該裝置上的應用程式。
建議應用程式在收到這項呼叫後,與應用程式伺服器進行完整同步。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-16 (世界標準時間)。
[null,null,["上次更新時間:2025-08-16 (世界標準時間)。"],[],[],null,["To receive simple downstream messages, each client app needs to implement the\nmethods on the\n[firebase::messaging::Listener](/docs/reference/cpp/class/firebase/messaging/listener)\nAPI.\n\nInitialize FCM\n\nBefore you can use FCM to get access to your registration token or receive messages it must be initialized.\n\nTo initialize FCM, call\n[::firebase::messaging::Initialize](/docs/reference/cpp/namespace/firebase/messaging#initialize)\nand supply it with your\n[::firebase::App](/docs/reference/cpp/class/firebase/app)\nobject as well as an implementation of the\n[::firebase::messaging::Listener](/docs/reference/cpp/class/firebase/messaging/listener)\nclass. \n\n```c++\nMyListener my_listener_implementation;\n::firebase::messaging::Initialize(app, &my_listener_implementation);\n```\n\nAccess the registration token\n\nOn initial startup of your app, the FCM SDK generates a registration\ntoken for the client app instance. If you want to target single devices, or\ncreate device groups for FCM, you'll need to access this token.\n\nYou can access the token's value through the\n[::firebase::messaging::Listener::OnTokenReceived](/docs/reference/cpp/class/firebase/messaging/listener#ontokenreceived)\nvirtual function. \n\n```c++\nvoid OnTokenReceived(const char* token) {\n LogMessage(\"The registration token is `%s`\", token);\n\n // TODO: If necessary send token to application server.\n}\n```\n\nReceive and handle messages\n\nTo receive messages, your Listener class must implement the\n[OnMessage](/docs/reference/cpp/class/firebase/messaging/listener#onmessage)\nvirtual function.\n\nOverride `OnMessage`\n\nBy overriding the method\n[::firebase::messaging::Listener::OnMessage](/docs/reference/cpp/class/firebase/messaging/listener#onmessage),\nyou can perform actions based on the received message and get the message data: \n\n```c++\nvoid OnMessage(const ::firebase::messaging::Message& message) {\n LogMessage(TAG, \"From: %s\", message.from.c_str());\n LogMessage(TAG, \"Message ID: %s\", message.message_id.c_str());\n}\n```\n\nMessages can represent different kinds of incoming data. Most commonly,\nmessages are sent to the app after being initiated by the developer. Messages\nare also sent to you app to represent message sent events, message send error\nevents, and messages deleted events. These special events can be differentiated\nby checking the `Message::message_type` field.\n\nMessages Deleted\n\nSent to your app when the FCM server deletes pending messages.\n`Message::message_type` will be `\"deleted_messages\"`. Messages may be delete due\nto:\n\n1. Too many messages stored on the FCM server.\n\n This can occur when an app's servers send a bunch of non-collapsible\n messages to FCM servers while the device is offline.\n2. The device hasn't connected in a long time and the app server has\n recently (within the last 4 weeks) sent a message to the app on that\n device.\n\n It is recommended that the app do a full sync with the app\n server after receiving this call."]]