- Home /
How to send notification when there is a data change in firebase in Unity?
I have been googling this for a while but I couldn't get any closer. I'm developing a 2d mobile game in unity and I use Firebase database. What I want to do is a simple friend request system. Assume that there are users A and B. Also, there is a Users node in Firebase realtime database. When user A adds user B as a friend, id of user A is being written in FriendRequests node of User B. Then there should be a notification sent to User B even if the game is not running.
In short, I want to send notification when there is a data change in Firebase real time database in Unity even if the app is closed. How can I achieve that? Thank you in advance for any idea.
Answer by vickychaudhary8955 · Jan 07 at 06:14 AM
hlw man @serkantarakci did you get any answer for the problem you mentioned above actually i'm trying to achieve something similar to that for my game it would a great help if you will tell me something you did for that thing.
Hi @vickychaudhary8955 , one of my friends solved this. It's kinda little complicated but let me try to describe how did we do that.
We used "Firebase Cloud Messaging HTTP protocol". Using that, we are able to send notifications to certain devices. To do that we need to know token of that device. So firstly, we imported Firebase Cloud Messaging to our game. Thanks to that we can get device tokens. Like:
var token = await Firebase.Messaging.FirebaseMessaging.GetTokenAsync();
For example, in our game users sign up and create an account. Once user creates account we store the token in users information in Firebase Realtime Database. Then there is a page with list of users. You can send friend request to those users. Once you send request, notification is also sent to that device. Like:
public void AddFriendRequest()
{
StartCoroutine(AddFriendNotification());
//other codes that you want to perform...
}
public IEnumerator AddFriendNotification()
{
string url = "https://fcm.googleapis.com/fcm/send";
var body = "{\"to\":\""+"HERE YOU SHOULD USE THE TOKEN"+"\",\"priority\":\"high\",\"notification\":{\"body\":\""+"YOU CAN WRITE HERE YOUR USERNAME*"+" "+ "YOU CAN WRITE HERE ADDED YOU AS A FRIEND**" + "\",\"title\":\""+ "YOU CAN WRITE HERE THE TITLE SOMETHING LIKE FRIEND REQUEST***" + "\"}}";
var request = new UnityWebRequest(url, "POST");
byte[] bodyRaw = Encoding.UTF8.GetBytes(body);
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("Authorization", "key=You can find your key under: Firebase Project Settings> Cloud Messaging");
yield return request.SendWebRequest();
}
After using that function in a button once you click that, the notification is sent to other device like:
FRIEND REQUEST (**Title) Username (Sender name) Added You As Friend()
I hope it helps. I know it may not be the best solution but I couldn't find any helpful info on the internet and we somehow solved our issue by that.
Your answer
Follow this Question
Related Questions
I have code related to adding audio. 0 Answers
WHY UNITY HAVE THIS PROBLEM ? 0 Answers
How to use the Right stick of the controller like Input.mousePosition 0 Answers
Unity2D rotate object with limits 0 Answers