简体   繁体   中英

How to alert device in real time for Android application?

Currently, I am using Firebase to create a real-time game in Android. A user from device 1 can "start a room" and this data would be updated in Firebase as a game object. The device 1 would switch to a waiting fragment waiting for another user to join the room.

Once player two from device 2 joins the game (adding their information to the Firebase game object), I want to switch both devices' screen to the game screen. How can I alert device 1 that player 2 has joined, and switch both of the devices to the game screen at the same time?

There are tow mechanism to do this thing,

1. By Firebase Listener: You can do it by creating the listener for the room when any data updated with that node then you get event throw which you can start the game,
Example: Like I created one room and added the listener to that node when any other user join you will get the event action, throw this you can start the game or transfer data to game.

2. By sending Notification: You can send notifications to them which you are joining with. (This is useful when you are in kill state)
Example:
From device 'A' you creating the room with data what you want, Please one extra data called device token In device token, you need to store device token of the device for which you creating room.

When any other device like 'B' will come to join the room of device B then send the notification from Device 'B' to device 'A' token from the created room. at that time switch data from room to game.

Note: This is helpful when room crating device is not opened the app

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM