firebase::messaging::PollableListener

#include <messaging.h>

A listener that can be polled to consume pending Messages.

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

Constructors and Destructors

PollableListener()
The default constructor.
~PollableListener()
The required virtual destructor.

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()

The default constructor.

~PollableListener

virtual  ~PollableListener()

The required virtual destructor.