1

I've implemented the Firebase messaging SDK for Unity (only Android atm). I've been able to receive the push notification with no problem.

The problem I do have, is that the data inside the message is empty. Here is my C# code:

    void Start()
{
    FirebaseMessaging.TokenReceived += FirebaseMessaging_TokenReceived;
    FirebaseMessaging.MessageReceived += FirebaseMessaging_MessageReceived;
}

private void FirebaseMessaging_TokenReceived(object sender, TokenReceivedEventArgs args)
{
    Debug.Log("FirebasePushNotification - Received Registration Token: " + args.Token);
}

private void FirebaseMessaging_MessageReceived(object sender, MessageReceivedEventArgs args)
{
    Debug.Log("FirebasePushNotification - Received a new message!");
    Debug.Log(GetFirebaseMessageString(args.Message));
}

/// <summary>
/// Used for debug
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
private string GetFirebaseMessageString(FirebaseMessage msg)
{
    var builder = new StringBuilder();

    builder.AppendLine("############# Firebase Message #############");
    builder.AppendLine(string.Format("Message Id: ", msg.MessageId));
    builder.AppendLine(string.Format("Message Type: ", msg.MessageType));
    builder.AppendLine(string.Format("Priority: ", msg.Priority));
    builder.AppendLine(string.Format("From: ", msg.From));
    builder.AppendLine(string.Format("To: ", msg.To));
    builder.AppendLine(string.Format("Collapse Key: ", msg.CollapseKey));
    builder.AppendLine(string.Format("Error: ", msg.Error));
    builder.AppendLine(string.Format("Error Description: ", msg.ErrorDescription));
    builder.AppendLine(string.Format("Time To Live: ", msg.TimeToLive));
    builder.AppendLine(string.Format("Raw Data: ", msg.RawData));
    builder.AppendLine(string.Format("Notification Opened: ", msg.NotificationOpened));
    if (msg.Notification != null)
    {
        builder.AppendLine(string.Format(".Notification Title: ", msg.Notification.Title));
        builder.AppendLine(string.Format(".Notification Tag: ", msg.Notification.Tag));
        builder.AppendLine(string.Format(".Notification Body: ", msg.Notification.Body));
        builder.AppendLine(string.Format(".Notification ClickAction: ", msg.Notification.ClickAction));
        builder.AppendLine(string.Format(".Notification Sound: ", msg.Notification.Sound));
    }
    builder.AppendLine("############# End #############");

    return builder.ToString();
}

All the parameters under #### Firebase Message #### are empty. Any ideas? Thanks!

2

1 Answer 1

0

The problem seems due to C# code.

The String.Format required a brace with the index of parameter that should be inserted on string (indexes start at 0). In your code:

string.Format("Message Id: {0}", msg.MessageId)

form more info about String.Format look at https://msdn.microsoft.com/it-it/library/system.string.format(v=vs.110).aspx

So the fixed code will be:

/// <summary>
/// Used for debug
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
private string GetFirebaseMessageString(FirebaseMessage msg)
{
    var builder = new StringBuilder();

    builder.AppendLine("############# Firebase Message #############");
    builder.AppendLine(string.Format("Message Id: {0}", msg.MessageId));
    builder.AppendLine(string.Format("Message Type: {0}", msg.MessageType));
    builder.AppendLine(string.Format("Priority: {0}", msg.Priority));
    builder.AppendLine(string.Format("From: {0}", msg.From));
    builder.AppendLine(string.Format("To: {0}", msg.To));
    builder.AppendLine(string.Format("Collapse Key: {0}", msg.CollapseKey));
    builder.AppendLine(string.Format("Error: {0}", msg.Error));
    builder.AppendLine(string.Format("Error Description: {0}", msg.ErrorDescription));
    builder.AppendLine(string.Format("Time To Live: {0}", msg.TimeToLive));
    builder.AppendLine(string.Format("Raw Data: {0}", msg.RawData));
    builder.AppendLine(string.Format("Notification Opened: {0}", msg.NotificationOpened));
    if (msg.Notification != null)
    {
        builder.AppendLine(string.Format(".Notification Title: {0}", msg.Notification.Title));
        builder.AppendLine(string.Format(".Notification Tag: {0}", msg.Notification.Tag));
        builder.AppendLine(string.Format(".Notification Body: {0}", msg.Notification.Body));
        builder.AppendLine(string.Format(".Notification ClickAction: {0}", msg.Notification.ClickAction));
        builder.AppendLine(string.Format(".Notification Sound: {0}", msg.Notification.Sound));
    }
    builder.AppendLine("############# End #############");

    return builder.ToString();
}

testing it from Firebase console works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.