I'm testing my multiplayer implementation using Google Play Game Services with 4 distinct devices. Everything is working fine but two things:
onInvitationReceived sometimes not firing (only sometimes ¿?)
Only in one device (Galaxy Ace), and only when is it who receives the invitation, then both players (inviter and invited) keeps waiting in the waiting room, so onActivity result is never fired. In waiting room I read "Invitation accepted" for both players, but it never closes...
Any advices?
Usually, when listeners are not firing, it's because they are getting garbage-collected. This happens if you use an anonymous listener (new Listener() { .... }). To avoid this, make your Activity implement the desired listener interface, and pass in this as the listener to GamesClient methods.
For more info, take a look at our troubleshooting guide, in the "Listeners not Called" section.
As for onActivityResult, if you are overring BaseGameActivity, make sure you are keeping the super.onActivityResult(...) line on your implementation of onActivityResult.
Related
I made an Android app which receives realtime data from Firebase database. In my main Activity the user has to log in with e-mail and password and by success it opens a new Activity, which receives data.
It seems to be, that when the user leaves my app, the Firebase connection is still established. That is not good for the battery.
How is the way to manage the connection for closing and reopen the app? I think I need to use onPause() and onResume(). I found something like goOffline(), but I cannot found this method in the new Firebase.
Yes, you may use the activity lifecycle methods like onPause(), onStop() on onDestroy() to close your connection.
Also, it seems Firebase still has a goOffline() method - https://www.firebase.com/docs/android/api/#firebase_goOffline.
An alternative to using the goOffline() and goOnLine() methods is to remove ValueEventListeners and ChildEventListeners when they are no longer needed. My experience (with SDK 3.X only) is that Firebase will automatically disconnect after about 1 minute when then are no listeners registered and no other reason to be connected, such as a pending write.
I've recently added Firebase database to my App, but noticed occasionally high battery usage, and it appears to be linked to my App keeping the radio active (radio active for 1 hour 30 mins in 10 hours, but app usage approx 5 minutes).
I believe that this is linked to Firebase database as I have disabled the in-app purchase broadcast receiver which is the only other network element. I don't have any open listeners (I'm using single-value events), but some of the transactions are mutable, so it's possible one of them has failed to complete, and is regularly re-trying. Database persistence is off.
I'm currently testing the following simple addition to pause/resume (got to get some code in the answer :-) ) :
#Override
protected void onResume()
{
super.onResume();
if (FirebaseDatabase.getInstance() != null)
{
FirebaseDatabase.getInstance().goOnline();
}
}
#Override
public void onPause() {
super.onPause();
if(FirebaseDatabase.getInstance()!=null)
{
FirebaseDatabase.getInstance().goOffline();
}
}
And so far, so good. ** The main thing to note is switching activities, requires you to have this in every app that uses Firebase (I had a sign-in activity which stopped functioning as it was reading the username from the database) **
5/12/16 - update after a couple of weeks of testing. The high battery use returned yesterday. I'm testing primarily on Lollipop, and there's loads of complaints about the radio being left open for other apps. Others testing my App on Android M don't report any problems, so it's possibly a Lollipop issue. I'm going to continue to test, this time trying to remove unnecessary transactions to see if this helps... Hope this helps someone.
I am working on an android app where I am supposed to log the flow in the Localytics(same like google analytics); that means how a user is walking through in my app. But flow in my app seems to be behaving weird because there are few Activities which can never be reached by certain Activities. But this flow is being logged in the Localytics. I followed the documentation accurately and events are being logged in fine.
I am writing these 3 lines of code in every activity's onResume() method as documentation says
onResume()
{
super.onResume();
Localytics.openSession();
Localytics.tagScreen("Splash");
Localytics.upload();
}
Now I have a couple of questions about these line of codes.
1. Is it the right approach to write Localytics.openSession(); in EVERY activity's onResume() method OR it should ONLY BE CALLED ONCE in the Splash activity?
2. Do I have to write Localytics.closeSession(); a method in every activity, for the Localytics to work properly?
3. Where can I find detailed information about what these methods do and how? Because There is not enough information in the API section of this Website(www.localytics.com).
My main concern and the priority is to make sure the flow of activities should be logged properly. So if someone has faced the same problem and found a working solution. Please share it here.
I'm creating a multiplayer tic tac toe game and it works for the most part, but when it's time to rematch I'm getting less than desirable functionality.
So originally I used the same implementation of Games.TurnBasedMultiplayer.rematch as the TBMPSkeleton sample project. Basically after calling Games.TurnBasedMultiplayer.finish, I check whether or not the match can be rematched by calling match.canRematch() during the subsequent callback. If match.canRematch() returns true, then I call Games.TurnBasedMultiplayer.rematch. Both, when I call finish and when I call rematch, the onTurnBasedMatchReceived callback gets called on the opposing client device and from there I check the match object for the rematchId. If it's not null, then I reset the game.
The problem I'm having is that, after the winning player has requested a rematch and then takes his/her turn, the opposing player receives an invite to the new match, but the onTurnBasedMatchReceived callback doesn't get called. I don't want the losing player to have to leave my game in order to accept or dismiss the invitation.
So is there a way to have my app handle the invitation notification without forcing the player to have to open the system's notification gui? Should I scrap the turn based multiplayer API in favor of it's real-time counterpart?
I realized that I didn't have a listener registered for invitations. After registering one, I was able to achieve my desired functionality. I'm relieved that it's working, but it would have been nice to notice that much sooner...
When I call takeTurn(), I will get a call to onTurnBasedMatchUpdated() on the same device that called takeTurn(). But I will not get the call to onTurnBasedMatchUpdated() on the other device that is waiting for its turn. Instead, Game Services will alert the device that it is their turn to play in the notification bar. I was hoping that, if the device remained in-game, that the game would continue to catch onTurnBasedMatchUpdated() when the other player calls takeTurn(). So how do I prevent the notification and simply handle the call?
As Mannan pointed out, it is onTurnBasedMatchReceived() that is called when an opponent takes their turn. It is also called when an opponent connects to your match for the first time. While this function is required to be defined for an "OnTurnBasedMatchUpdatedListener" it is not actually called for that listener. You must ALSO implement "OnTurnBasedMatchUpdateReceivedListener". Further, after implementing it, you must then REGISTER it (I do so after signing in) with getGamesClient().registerMatchUpdateListener(this). Seriously though, where is all the documentation for this? Am I missing something?
When a player's turn is arrived, following callback is called,
onTurnBasedMatchReceived(TurnBasedMatch match)
and in this callback, you can get info about the turn and its data from match parameter.
I still dont know why onTurnBasedMatchUpdated() is not called.
I am facing problem in signing in using the google real time muleiplayer api's even after following all steps throughly as given in the documentation. Following is the link from where I have referred: https://developers.google.com/games/services/android/init.
I am not getting any error but my main activity extends BaseGameActivity when i start my game it calls onSignInFailed() automatically but i am calling the beginUserInitiatedSignIn() method from BaseGameActivity which is used for initialization of sign in on the button click none of the overridden methods gets called not even onSignInFailed() nor onConnectionFailed().
It's normal for it to call onSignInFailed on startup if have never signed in before. That's working as intended. Then, you should call beginUserInitiatedSignIn when your button is clicked, and then either onSignInFailed on onSignInSucceeded should be called.
I'd suggest putting some print statements around your code and BaseGameActivity to figure out what methods are being called to figure out what's happening.
By the way, the most common cause for sign-in errors is having a Client ID that's incorrectly set up. I'd recommend taking a look at the troubleshooting guide to see if there is a problem with your setup:
https://developers.google.com/games/services/android/troubleshooting