在 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.
}
接收和处理消息
如需接收消息,您的监听器类必须实现 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 周内)向该设备上的应用发送了一条消息。
建议在收到此调用后为应用执行与应用服务器的完全同步。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-16。
[null,null,["最后更新时间 (UTC):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."]]