Stay organized with collections
Save and categorize content based on your preferences.
firebase::messaging::PollableListener
#include <messaging.h>
A listener that can be polled to consume pending Message
s.
Summary
This class is intended to be used with applications that have a main loop that frequently updates, such as in the case of a game that has a main loop that updates 30 to 60 times a second. Rather than respond to incoming messages and tokens via the OnMessage
virtual function, this class will queue up the message internally in a thread-safe manner so that it can be consumed with PollMessage
. For example:
::firebase::messaging::PollableListener listener;
::firebase::messaging::Initialize(app, &listener);
while (true) {
std::string token;
if (listener.PollRegistrationToken(&token)) {
LogMessage("Received a registration token");
}
::firebase::messaging::Message message;
while (listener.PollMessage(&message)) {
LogMessage("Received a new message");
}
// Remainder of application logic...
}
Inheritance
Inherits from:
firebase::messaging::Listener
Public functions
|
OnMessage(const Message & message)
|
virtual void
An implementation of OnMessage which adds the incoming messages to a queue, which can be consumed by calling PollMessage .
|
OnTokenReceived(const char *token)
|
virtual void
An implementation of OnTokenReceived which stores the incoming token so that it can be consumed by calling PollRegistrationToken .
|
PollMessage(Message *message)
|
bool
Returns the first message queued up, if any.
|
PollRegistrationToken(std::string *token)
|
bool
Returns the registration key, if a new one has been received.
|
Public functions
OnMessage
virtual void OnMessage(
const Message & message
)
An implementation of OnMessage
which adds the incoming messages to a queue, which can be consumed by calling PollMessage
.
OnTokenReceived
virtual void OnTokenReceived(
const char *token
)
An implementation of OnTokenReceived
which stores the incoming token so that it can be consumed by calling PollRegistrationToken
.
PollMessage
bool PollMessage(
Message *message
)
Returns the first message queued up, if any.
If one or more messages has been received, the first message in the queue will be popped and used to populate the message
argument and the function will return true
. If there are no pending messages, false
is returned. This function should be called in a loop until all messages have been consumed, like so:
::firebase::messaging::Message message;
while (listener.PollMessage(&message)) {
LogMessage("Received a new message");
}
Details |
Parameters |
message
|
The Message struct to be populated. If there were no pending messages, message is not modified.
|
|
Returns
|
Returns true if there was a pending message, false otherwise.
|
PollRegistrationToken
bool PollRegistrationToken(
std::string *token
)
Returns the registration key, if a new one has been received.
When a new registration token is received, it is cached internally and can be retrieved by calling PollRegistrationToken
. The cached registration token will be used to populate the token
argument, then the cache will be cleared and the function will return true
. If there is no cached registration token this function retuns false
.
std::string token;
if (listener.PollRegistrationToken(&token)) {
LogMessage("Received a registration token");
}
Details |
Parameters |
token
|
A string to be populated with the new token if one has been received. If there were no new token, the string is left unmodified.
|
|
Returns
|
Returns true if there was a new token, false otherwise.
|
PollableListener
PollableListener()
~PollableListener
virtual ~PollableListener()
The required virtual destructor.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-01-23 UTC.
[null,null,["Last updated 2024-01-23 UTC."],[],[],null,["# firebase::messaging::PollableListener Class Reference\n\nfirebase::messaging::PollableListener\n=====================================\n\n\n`#include \u003cmessaging.h\u003e`\n\nA listener that can be polled to consume pending [Message](/docs/reference/cpp/struct/firebase/messaging/message#structfirebase_1_1messaging_1_1_message)s.\n\nSummary\n-------\n\nThis class is intended to be used with applications that have a main loop that frequently updates, such as in the case of a game that has a main loop that updates 30 to 60 times a second. Rather than respond to incoming messages and tokens via the `OnMessage` virtual function, this class will queue up the message internally in a thread-safe manner so that it can be consumed with `PollMessage`. For example: \n\n```c++\n::firebase::messaging::PollableListener listener;\n::firebase::messaging::Initialize(app, &listener);\n\nwhile (true) {\n std::string token;\n if (listener.PollRegistrationToken(&token)) {\n LogMessage(\"Received a registration token\");\n }\n\n ::firebase::messaging::Message message;\n while (listener.PollMessage(&message)) {\n LogMessage(\"Received a new message\");\n }\n\n // Remainder of application logic...\n} \n```\n\n\u003cbr /\u003e\n\n### Inheritance\n\nInherits from: [firebase::messaging::Listener](/docs/reference/cpp/class/firebase/messaging/listener)\n\n| ### Constructors and Destructors ||\n|---|---|\n| [PollableListener](#classfirebase_1_1messaging_1_1_pollable_listener_1aada54b5d624c0593b2521ba0a12231af)`()` The default constructor. ||\n| [~PollableListener](#classfirebase_1_1messaging_1_1_pollable_listener_1a5a43c4b5802989eab4081bf513d09c6a)`()` The required virtual destructor. ||\n\n| ### Public functions ||\n|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [OnMessage](#classfirebase_1_1messaging_1_1_pollable_listener_1aeb924594f29e81b9e90eafc450b9813e)`(const `[Message](/docs/reference/cpp/struct/firebase/messaging/message#structfirebase_1_1messaging_1_1_message)` & message)` | `virtual void` An implementation of `OnMessage` which adds the incoming messages to a queue, which can be consumed by calling `PollMessage`. |\n| [OnTokenReceived](#classfirebase_1_1messaging_1_1_pollable_listener_1acdd9f0597ba7687863bab94d9867f95f)`(const char *token)` | `virtual void` An implementation of `OnTokenReceived` which stores the incoming token so that it can be consumed by calling `PollRegistrationToken`. |\n| [PollMessage](#classfirebase_1_1messaging_1_1_pollable_listener_1a9ad784d05e512afb638be6081c9df3f5)`(`[Message](/docs/reference/cpp/struct/firebase/messaging/message#structfirebase_1_1messaging_1_1_message)` *message)` | `bool` Returns the first message queued up, if any. |\n| [PollRegistrationToken](#classfirebase_1_1messaging_1_1_pollable_listener_1a39915cdfda32fc52e7930288ddaac202)`(std::string *token)` | `bool` Returns the registration key, if a new one has been received. |\n\nPublic functions\n----------------\n\n### OnMessage\n\n```c++\nvirtual void OnMessage(\n const Message & message\n)\n``` \nAn implementation of `OnMessage` which adds the incoming messages to a queue, which can be consumed by calling `PollMessage`. \n\n### OnTokenReceived\n\n```c++\nvirtual void OnTokenReceived(\n const char *token\n)\n``` \nAn implementation of `OnTokenReceived` which stores the incoming token so that it can be consumed by calling `PollRegistrationToken`. \n\n### PollMessage\n\n```c++\nbool PollMessage(\n Message *message\n)\n``` \nReturns the first message queued up, if any.\n\nIf one or more messages has been received, the first message in the queue will be popped and used to populate the `message` argument and the function will return `true`. If there are no pending messages, `false` is returned. This function should be called in a loop until all messages have been consumed, like so: \n\n```c++\n::firebase::messaging::Message message;\nwhile (listener.PollMessage(&message)) {\n LogMessage(\"Received a new message\");\n}\n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Details ||\n|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Parameters | |-----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `message` | The [Message](/docs/reference/cpp/struct/firebase/messaging/message#structfirebase_1_1messaging_1_1_message) struct to be populated. If there were no pending messages, `message` is not modified. | |\n| **Returns** | Returns `true` if there was a pending message, `false` otherwise. |\n\n### PollRegistrationToken\n\n```c++\nbool PollRegistrationToken(\n std::string *token\n)\n``` \nReturns the registration key, if a new one has been received.\n\nWhen a new registration token is received, it is cached internally and can be retrieved by calling `PollRegistrationToken`. The cached registration token will be used to populate the `token` argument, then the cache will be cleared and the function will return `true`. If there is no cached registration token this function retuns `false`. \n\n```c++\nstd::string token;\nif (listener.PollRegistrationToken(&token)) {\n LogMessage(\"Received a registration token\");\n}\n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Details ||\n|-------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Parameters | |---------|----------------------------------------------------------------------------------------------------------------------------------| | `token` | A string to be populated with the new token if one has been received. If there were no new token, the string is left unmodified. | |\n| **Returns** | Returns `true` if there was a new token, `false` otherwise. |\n\n### PollableListener\n\n```c++\n PollableListener()\n``` \nThe default constructor. \n\n### \\~PollableListener\n\n```c++\nvirtual ~PollableListener()\n``` \nThe required virtual destructor."]]