PaffAttack
PaffAttack

Reputation: 49

I need to use Firebase Cloud Messaging on a DESKTOP unity application

I have the things installed on my Unity project, but I need to know if I am able to send push notifications to a stand alone desktop (windows or mac) application running Unity.

I know that the realtime database says it is for IOS / Android only but I have personally gotten it to work on a desktop app like what I am making now. Does this work with Cloud Messaging???

Please help.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase.Messaging;

public class NotificationManager : MonoBehaviour
{


    public void OnMessageReceived(object sender, MessageReceivedEventArgs e)
    {
        Debug.Log("From: " + e.Message.From);
        Debug.Log("Message ID: " + e.Message.MessageId);
    }


}

I cannot send test notifications from firebase console as it doesn't recognize anything yet, but I haven't deployed it or built it at all. I am just trying to test functionality in the editor.

Upvotes: 1

Views: 2997

Answers (1)

Patrick Martin
Patrick Martin

Reputation: 3131

Unfortunately, firebase cloud messaging does not have a desktop SDK in Unity (see this link for a list of supported desktop products, which it appears that you've found).

Depending on your use case though, you may be able to work around this with Realtime Database. If you just want to reliably send messages in real time to running instances of your application, you could do something like: 1) Authenticate with Firebase Auth (I'd recommend starting with anonymous authentication or email address/password).

2) Have some node in your RTDB that's only readable by the receiver, but any logged in user can write to (make sure users can't stomp each other's messages, either another restriction on the writing UID or just making sure you can't overwrite a message in your rules).

3) Attach a listener to a user's RTDB node to listen to new messages coming in.

4) Write to a node to send a message.

So, I setup a database ruleset like this in RTDB:

{
  "rules": {
    ".read": false,
    ".write": false,
    "messages": {
      "$read_uid": {
        "$write_uid": {
          ".read": "$write_uid == auth.uid || $read_uid == auth.uid",
          ".write": "$write_uid == auth.uid || $read_uid == auth.uid"
        },
        ".read": "$read_uid == auth.uid",
        ".write": "$read_uid == auth.uid || newData.val() == auth.uid"
      }
    }
  }
}

Which lets you read/write anything under messages/your_uid, or messages/some_uid/your_uid.

You can create your node with:

public class Register : MonoBehaviour
{
    public void RegisterUser(string username, string password)
    {
        FirebaseAuth.DefaultInstance.CreateUserWithEmailAndPasswordAsync(username, password).ContinueWithOnMainThread(
            task =>
            {
                if (task.IsCompleted)
                {
                    var databaseReference = FirebaseDatabase.DefaultInstance.RootReference;
                    databaseReference.Child("messages").SetValueAsync(task.Result.UserId);
                }
            });
    }
}

You can listen to changes to this via:

public class ListenForMessages : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        FirebaseDatabase.DefaultInstance
            .GetReference($"/messages/{FirebaseAuth.DefaultInstance.CurrentUser.UserId}")
            .ValueChanged += HandleValueChanged;
    }

    private void HandleValueChanged(object sender, ValueChangedEventArgs e)
    {
        Debug.Log("Do something with " + e.Snapshot);
    }
}

Then sending a message becomes:

public class SendMessage : MonoBehaviour
{
    public void SendMessageToUser(string uid, string message)
    {
        FirebaseDatabase.DefaultInstance
            .GetReference($"/messages/{uid}/{FirebaseAuth.DefaultInstance.CurrentUser.UserId}").Push()
            .SetValueAsync(message);
    }
}

This is not nearly a complete set of code, and I've verified that it compiles (and the database rules in the simulator appear correct). But I haven't built a full test app to verify this all.

Hopefully it helps, and I apologize if this doesn't meet your current needs. Feel free to send a feature request for FCM on desktop via the normal support channel and follow along on the Unity GitHub page or the open source C++ SDK to follow up to the moment progress on Firebase for games (including desktop support).

--Patrick

Upvotes: 2

Related Questions