在 Unity 客户端应用上接收消息
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
接收和处理消息
如需接收消息,您的应用必须为 Firebase.Messaging.FirebaseMessaging.MessageReceived
事件处理程序分配一个回调函数。
MessageReceived
个事件
通过对将回调函数分配给 Firebase.Messaging.FirebaseMessaging.MessageReceived
进行重写,您可以根据接收到的消息执行操作并获取消息数据:
public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e) {
UnityEngine.Debug.Log("From: " + e.Message.From);
UnityEngine.Debug.Log("Message ID: " + e.Message.MessageId);
}
消息可用于表示不同种类的传入数据。在大多数情况下,消息会在开发者发起后发送至应用。此外,您的应用还会收到用于表示消息已发送事件、消息发送错误事件及消息已删除事件的消息。您可以通过检查 MessageType
字段来区分这些特殊事件。
消息已删除
当 FCM 服务器删除待处理消息时发送至您的应用。MessageType
将是 "deleted_messages"
。如果发生以下情况,消息可能会被删除:
FCM 服务器上存储的消息过多。
如果应用服务器在设备离线时向 FCM 服务器发送大量不可折叠的消息,可能会出现这种情况。
设备长时间未连接,且应用服务器近期(过去 4 周内)向该设备上的应用发送了一条消息。
建议在收到此调用后为应用执行与应用服务器的完全同步。
发送事件
当上行消息成功发送至 FCM 时调用。MessageType
将是 "send_event"
。
发送错误
当发送上行消息出错时调用。
MessageType
将是 "send_error"
。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-12。
[null,null,["最后更新时间 (UTC):2025-08-12。"],[],[],null,["Receive and handle messages\n\nTo receive messages, your app must assign a callback to the\n[Firebase.Messaging.FirebaseMessaging.MessageReceived](/docs/reference/unity/class/firebase/messaging/firebase-messaging#messagereceived)\nevent handler.\n\n`MessageReceived` Event\n\nBy overriding assigning a callback to\n[Firebase.Messaging.FirebaseMessaging.MessageReceived](/docs/reference/unity/class/firebase/messaging/firebase-messaging#messagereceived)\nyou can perform actions based on the received message and get the message data: \n\n```c#\npublic void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e) {\n UnityEngine.Debug.Log(\"From: \" + e.Message.From);\n UnityEngine.Debug.Log(\"Message ID: \" + e.Message.MessageId);\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 `MessageType` field.\n\nMessages Deleted\n\nSent to your app when the FCM server deletes pending messages.\n`MessageType` will be `\"deleted_messages\"`. Messages may be deleted if:\n\n1. Too many messages are 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.\n\nSend Event\n\nCalled when an upstream message has been successfully sent to FCM.\n`MessageType` will be `\"send_event\"`.\n\nSend Error\n\nCalled when there was an error sending an upstream message.\n`MessageType` will be `\"send_error\"`."]]