Skip to content

Commit

Permalink
Merge pull request #1403 from darshanio/task/firebase-push-fix
Browse files Browse the repository at this point in the history
[Android] Verified if the FirebaseApp is not already running before initialization.
  • Loading branch information
aritchie authored Feb 28, 2024
2 parents 0d7831f + ffd1b57 commit 0b00651
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/Shiny.Push/Platforms/Android/PushManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,26 @@ void DoInit()

if (this.config.UseEmbeddedConfiguration)
{
FirebaseApp.InitializeApp(this.platform.AppContext);
if (FirebaseApp.Instance == null)
throw new InvalidOperationException("Firebase did not initialize. Ensure your google.services.json is property setup. Install the nuget package `Xamarin.GooglePlayServices.Tasks` into your Android head project, restart visual studio, and then set your google-services.json to GoogleServicesJson");
if (!this.IsFirebaseAappAlreadyInitialized())
{
FirebaseApp.InitializeApp(this.platform.AppContext);
if (FirebaseApp.Instance == null)
throw new InvalidOperationException("Firebase did not initialize. Ensure your google.services.json is property setup. Install the nuget package `Xamarin.GooglePlayServices.Tasks` into your Android head project, restart visual studio, and then set your google-services.json to GoogleServicesJson");
}
}
else
{
var options = new FirebaseOptions.Builder()
.SetApplicationId(this.config.AppId)
.SetProjectId(this.config.ProjectId)
.SetApiKey(this.config.ApiKey)
.SetGcmSenderId(this.config.SenderId)
.Build();

FirebaseApp.InitializeApp(this.platform.AppContext, options);
if (!this.IsFirebaseAappAlreadyInitialized())
{
var options = new FirebaseOptions.Builder()
.SetApplicationId(this.config.AppId)
.SetProjectId(this.config.ProjectId)
.SetApiKey(this.config.ApiKey)
.SetGcmSenderId(this.config.SenderId)
.Build();

FirebaseApp.InitializeApp(this.platform.AppContext, options);
}
}

ShinyFirebaseService.NewToken = async token =>
Expand Down Expand Up @@ -266,6 +272,22 @@ await this.services
this.initialized = true;
}

bool IsFirebaseAappAlreadyInitialized()
{
var isAppInitialized = false;
var firebaseApps = FirebaseApp.GetApps(this.platform.AppContext);
foreach (var app in firebaseApps)
{
if (string.Equals(app.Name, FirebaseApp.DefaultAppName))
{
isAppInitialized = true;
break;
}
}

return isAppInitialized;
}


// protected virtual void TryTriggerNotification(RemoteMessage message)
// {
Expand Down

0 comments on commit 0b00651

Please sign in to comment.