dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.98k stars 1.71k forks source link

.NET 8 MAUI Android - Storage.Preferences returns default value when executed whilst the application isn't open/running #23837

Open tommyw1996 opened 1 month ago

tommyw1996 commented 1 month ago

Description

Code

bool showNotification = Preferences.Get(UserPreferenceKeys.ShowNotification, true); bool showNotificationUsingDefault = Preferences.Default.Get(UserPreferenceKeys.ShowNotification, true);

Issue

Returns default value when executed whilst the application isn't open/running.

Scenario

My application is deployed via an ARM template and installed onto a local Android device via an APK file.

I'm currently trying to access some application preferences via a firebase messaging service using Firebase.Messaging.FirebaseMessagingService, specifically inside the OnMessageReceived method. I use these to determine if a notification should be shown or not. This method can be triggered in the background whilst the application isn't open/running, but when this happens, the preferences are returning the default values passed in, which in this case is true.

If I set my preference to false from inside of the application, when the application is reloaded, I can see that the preferences have persisted still and stored correctly.

Steps to Reproduce

  1. Create .NET 8 MAUI Android application
  2. Add some code to call Preferences.Get(UserPreferenceKeys.ShowNotification, true) or Preferences.Default.Get(UserPreferenceKeys.ShowNotification, true) that can be executed in the background whilst the application isn't open/running e.g. in my case here, this would be the firebase messaging service, explained more above in the description.
  3. Add some code to be able to view the returned preference value whilst the application isn't running e.g. in my case, I use the preference to determine if a notification can be shown or not.
  4. Add some code to set the preferences.

Link to public reproduction project repository

No response

Version with bug

8.0.70 SR7

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

34

Did you find any workaround?

No response

Relevant log output

No response

github-actions[bot] commented 1 month ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.