Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by serkantarakci · May 23, 2021 at 07:12 PM · unity 2dnotifications

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.

alt text

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image serkantarakci · Jan 07 at 08:08 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

123 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I have code related to adding audio. 0 Answers

WHY UNITY HAVE THIS PROBLEM ? 0 Answers

My trigger for my parent object gets triggered from child and parent,My parent object gets triggered from the child object 2 Answers

How to use the Right stick of the controller like Input.mousePosition 0 Answers

Unity2D rotate object with limits 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges