Cloud Messaging

Before you begin

Before you can use the Firebase Cloud Messaging, you will need to create a Firebase project, and add the Firebase Unity SDK packages to your Unity project. To use the API, you must first configure your game in the Firebase console. Import Firebase Unity SDK to your project. You only need  FirebaseMessaging.package. from the Firebase SDK.

Download resources

Important: During the process, you must download a google-services.json file. 

You can download this file again at any time.

API References

The Firebase Cloud Message library will be initialized when adding handlers for either the TokenReceived or MessageReceived events.Upon initialization, a registration token is requested for the client app instance. The app will receive the token with the OnTokenReceived event, which should be cached for later use. You'll need this token if you want to target this specific device for messages.

The entire setup looks like this:

AN_FirebaseMessaging.Initialize((token) => {
    m_tokenText.text = token;
    AN_Logger.Log("FB Token: " + m_tokenText.text);
});

In addition, you will need to register for the OnMessageReceived event if you want to be able to receive incoming messages.

AN_FirebaseMessaging.OnFbMessageReceived.AddListener((message) => {
    AN_Logger.Log("msg.CollapseKey " + message.CollapseKey);
    AN_Logger.Log("msg.Error " + message.Error);
    AN_Logger.Log("msg.ErrorDescription " + message.ErrorDescription);
    AN_Logger.Log("msg.MessageId " + message.MessageId);
    AN_Logger.Log("msg.MessageType " + message.MessageType);
    AN_Logger.Log("msg.NotificationOpened " + message.NotificationOpened);
    AN_Logger.Log("msg.Priority " + message.Priority);
    AN_Logger.Log("msg.RawData " + message.RawData);
    AN_Logger.Log("msg.TO " + message.To);
    
    foreach (KeyValuePair<string, string> data in message.Data) {
        AN_Logger.Log("data key: " + data.Key + " data value: " + data.Value);
    }
    
    AN_Logger.Log("msg.link " + message.Link);
    AN_Logger.Log("msg.TimeToLive " + message.TimeToLive);
    
    if (message.Notification == null)
        return;
    
    AN_Logger.Log("msg.Notification.Title " + message.Notification.Title);
    AN_Logger.Log("msg.TitleLocalizationKey " + message.Notification.TitleLocalizationKey);
    AN_Logger.Log("msg.Body " + message.Notification.Body);

    m_messageText.text = message.Notification.Body;
});