unity realtime game search while app is in background - c#

i have a unity mobile multiplayer game, using firebase for matchmaking,
when the users press the play button it adds them as available for machmake
mDatabaseRef.Child("matchMake").Child(auth.CurrentUser.UserId).Child("playerName").SetValueAsync(auth.CurrentUser.DisplayName);
and remove them when the user press the cancel game search button, but i also want to remove them when they close the app (even if they don't press the cancel game button) using
private void OnApplicationQuit()
{
removePlayer();
}
void OnApplicationPause(bool pause)
{
removePlayer();
}
public void removePlayer()
{
mDatabaseRef.Child("matchMake").Child(auth.CurrentUser.UserId).RemoveValueAsync();
}
so what i want is to keep them in matchmake if they add the app in background (and thinking of informing them about finding game with push notifications) but remove them if they close the app from App Switcher (or even if the system quit it to free up resources).
The problem is that while i can remove the "removeplayer" from OnApplicationPause that called when the app goes in background, it doesn't call the OnApplicationQuit if the app close from app switcher, this behaviour is kinda correct according to the documents
Warning: If the user suspends your application on a mobile platform, the operating system can quit the application to free up resources. In this case, depending on the operating system, Unity might be unable to call this method. On mobile platforms, it is best practice to not rely on this method to save the state of your application.
is there any way to do what i want ?
Thanks !!!

Firebase Realtime Database has so-called onDisconnect handlers that allow you to send a write instruction to the server while you are online, that the server then executes once it detects that the client has disconnected.
Somehow onDisconnect is not documented for the Unity, so I recommend checking the Android documentation to learn how onDisconnect works, and then implementing it based on the reference documentation for onDisconnect in Unity.

Related

Using MIDI in UWP

When using the UWP MIDI API with a physical MIDI device, the app hangs on exit and cannot be forcefully terminated neither from command line nor from the Task Manager.
This happens for all UWP apps that use MIDI, including the official example from MSDN, without modifications.
I have raised an issue on GitHub about this, but it does not seem to be a new problem. MIDI is supposedly broken since 2014.
Is there a way to circumvent this problem in UWP through other means? I'm writing in C#, but I am fluent in C++ and could use lower-level APIs, if they existed. I only need to get MIDI input from a selected MIDI device (keyboard).
I also tried using the winmm.dll as suggested in the related post (Getting signals from a MIDI port in C#), but this does not work for me. The midiInGetNumDevs() method returns 0 when used from a UWP app.
I am using M-Audio Fast Track USB sound card (connected via USB) and an M-Audio 88es Keystation MIDI Keyboard (connected via USB). I haven't had problems in desktop applications.
I have discovered a partial workaround where I move the deallocation code to the App Suspending event handler instead of having it in a destructor. However, the problem still arises when a device is unplugged during app's lifetime. If the MIDI device is plugged in while the app is running, the app closes without problems (with the workaround). If the MIDI device is plugged in before the app is run and remains plugged in while the app is running, the app closes without problems (with the workaround). Only if the device is plugged out while the app is running will the app hang on close, regardless of whether the deallocation code is done in the destructor or in the App Suspending handler.
I’m unable to reproduce this issue. I believe it could be related to the audio card’s driver.
Using the sample you mentioned, https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/MIDI
• App did not hang when I closed it.
• I was able to remove the cable during app lifecycle, without issue. I was still able to send and receive messages after reconnecting.
• I was able to plug in the device mid-lifecycle, without issue. I was still able to send and receive messages.
Although it's currently working from desktop, it’s required to be “universal” for it to work well in UWP. Is the audio card driver universal?
In our developing drivers doc, we see a note to Use the InfVerif tool to verify that your driver package's INF file is universal. https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/infverif.
In the app developer documentation, I see a note to test the device metadata. https://learn.microsoft.com/en-us/windows-hardware/drivers/devapps/step-4--test-device-metadata
Additional documentation related to your question:
Building UWP device apps
https://learn.microsoft.com/en-us/windows-hardware/drivers/devapps/the-workflow
Test the device metadata for your UWP device app
https://learn.microsoft.com/en-us/windows-hardware/drivers/devapps/step-4--test-device-metadata
Getting Started with Universal Windows drivers
https://learn.microsoft.com/en-us/windows-hardware/drivers/develop/getting-started-with-universal-drivers
MIDI sample
https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/MIDI
Releasing resources in app suspend
https://learn.microsoft.com/en-us/windows/uwp/launch-resume/suspend-an-app#release-resources
Use Dispose to suppress finalization
NOTE: depending on a finalizer will keep resources in memory longer than necessary. They won’t be released until a full GC is performed or when the process exits.
https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose

RequestAccessKindAsync from Desktop Bridge

I have Win32 desktop bridge application that uses background task to receive push notifications from WNS. I use UWP background task APIs over C++-WinRT
I'd like to be able to receive push notifications even when OS is in sleep so that it wakes up and the app handles push notification. By default OS does not wake up. It did only after manually changing settings value in System->Battery->See which apps are affecting your battery life->Click my app ->Uncheck Let Windows decide option (by default it is always checked) and check Allow the app to run background taks. Now I'd like to do this is manual work on code for better user experience.
RequestAccessKindAsync API allows to let user to change above setting value by showing popup notification to the user and I could do it without any problem in UWP C# sample app. But same code does not show pop notification from my desktop bridge over C++-WinRT. It simply returns false value for below code
auto result = co_await BackgroundExecutionManager::RequestAccessKindAsync(BackgroundAccessRequestKind::AlwaysAllowed,
L"App needs to use background to catch push notifications while device is in sleep");
Can anyone confirm that RequestAccessKindAsync API works from desktop bridge? If not then how I can make sure OS and the app will always be able to wake up from sleep when it receives push notification?
UPD: Raised request to enable this API from desktop brige here
This is a missing feature. When we designed this API a couple of releases ago we didn't consider desktop bridge apps would be calling it - but clearly there is a use case for supporting this. I have notified the team about this gap, but I'd also encourage you to log a feature request here: https://wpdev.uservoice.com/
Two possible workarounds:
(1) you could add a dummy/empty UWP foreground app to your desktop bridge app. You can then launch this on startup and request the background access from there. This will be a bit ugly, but you could make it look like a splash screen :-)
(2) you can instruct the user to go into the Settings app to set your app to always allowed. You can help them do that with a deep link to the battery save settings, but they will still need to manually flip the switch.

Cross-platform C# (Xamarin) - Execute Tasks while "Asleep" on iOS

We're writing a cross-platform C# "Xamarin" application; right now we're just targeting the iPad. The application has two features relevant to this question:
The app requires that the user authenticate to the app (in addition to logging onto the iPad)
The app connects to a remote device over Bluetooth
So the question becomes, what happens when the user switches to another app, which results in our app's OnSleep() being called. In OnSleep() we could immediately disconnect Bluetooth and log out the user, but that seems like poor usability, especially if they are just quickly checking some alarm that triggered or an instant message that came in.
For the sake of usability, we're thinking to have a 1-minute timeout; if the user does in fact pop out and quickly pop back into the app, we'd like things to simply continue on without any loss of communication or re-authentication.
If, in OnSleep(), I set up a timer using Xamarin.Forms.Device.StartTimer(), that timer does not fire while the app is "asleep".
What does it take to have a small background task/thread/process execute, even while the app is asleep? Something that simply waits 1 minute, and then shuts down the Bluetooth communication and sets a flag indicating re-authentication is needed?

PlayerPerfs not working when user kill task via task manager Android

i'm working on a game for android, i got a problem :
when i kill task via task manager on android the playerperf from unity not working, i had used OnApplicationQuit(), but it didn't saved when i kill task via task manager, but quit from quit button (which call Application.Quit()) works fine on android.
Anyone have the solution how to make playerperf work when user kill task via android task manager ? i'm using C# and Unity 5
In the Android activity lifecycle you can't really depend on your application being available to safely store data past its onPause state. The practice for native Android apps is to save any data you need by overriding the onPause() function.
In Unity, this maps to OnApplicationPause.
I wouldn't count on being able to safely guarantee any data can or will be saved past this point.

Xamarin Android App update

I've been developing android apps on Xamarin for 3months now, but I am facing a problem to update the application when a new release is available.
The application is not published to any app store(as we use it internally), I know Play store can do automatic updating, but I need to look for an update on our server, I would prefer if all this can be done silently(in the background).
here is my current code, this copy's the app to the device:
intent.SetData(Android.Net.Uri.Parse(#"http://xxxxx/downloads/app.test-signed.apk"));
StartActivity(intent);
this installs the app onto the device:
intent.SetDataAndType(Android.Net.Uri.FromFile(new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.Path +
"/app.test-signed.apk")), "application/vnd.android.package-archive");
StartActivity(intent);
but this way it asks to install (and I don't think this is the correct way), and doesn't start my app automatically after it was installed. And I am not sure how to wait until the app finished downloading before it tries to install.
I had a similar issue with an app which only gets used internally in my company. I didn't want the app going to the big wide world as the general public wouldn't have any use for it.
So, what we've done is publish the final product as 'Alpha release' in Google Play. You set up a Google Group with a set of e-mail addresses of devices that can access it, and once the app has been downloaded it will update just like any other app from Google Play.
There's an option to 'Promote' it to Beta or Production, but given that it never gets promoted it is never searchable on Google Play.
As Corey Ogburn mentionned, the silent install won't be possible without root.
Altough an answer was already posted to your question on Xamarin's forum.
On another note, following Dave's idea, you could set a Private Channel on Google Play. As you already know, Google Play offers easy automatic update, and with a Private Channel, you will be able to give special authorization on who has access to your app.
Read more on Private Channel here.

Categories

Resources