0

i am trying to implement fcm in my xamarin.android app using xamarin.forms. i am using the Plugin.FirebasePushNotification version 3.3.10 depending on this youtube tutorial: https://www.youtube.com/watch?v=7w2q2D6mR7g

i added Application.cs to my xamarin.android app:

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Plugin.FirebasePushNotification;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LearningWithCassidy.Droid
{
    [Application]
    public class MainApplication : Application
    {
        public MainApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer)
        {
        }

        public override void OnCreate()
        {
            base.OnCreate();

            //Set the default notification channel for your app when running Android Oreo
            if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
            {
                //Change for your default notification channel id here
                FirebasePushNotificationManager.DefaultNotificationChannelId = "FirebasePushNotificationChannel";

                //Change for your default notification channel name here
                FirebasePushNotificationManager.DefaultNotificationChannelName = "General";

                FirebasePushNotificationManager.DefaultNotificationChannelImportance = NotificationImportance.Max;
            }

            //If debug you should reset the token each time.
#if DEBUG
            FirebasePushNotificationManager.Initialize(this, true);
#else
            FirebasePushNotificationManager.Initialize(this, false);
#endif

            //Handle notification when app is closed here
            CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
            {


            };
        }
    }
}

and added this line to my MainActivity as mentioned:


protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
           
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
          
            LoadApplication(new App());

            FirebasePushNotificationManager.ProcessIntent(this, Intent);
        }

i also added the internet permission to my manifest file. but when i launch my app i get this exception:

**Java.Lang.IllegalStateException:** 'Default FirebaseApp is not initialized in this process com.companyname.learningwithcassidy. Make sure to call FirebaseApp.initializeApp(Context) first.'

i searched a lot and tried many things. i tried adding FirebaseApp.InitializeApp(this); to my mainactivity but nothing happened. what am i doing wrong? many answers correspond to android studio i really trie understanding the probelm but couldn't. thanks in advance

rana hd
  • 355
  • 3
  • 18
  • 1
    You can refer to [FirebaseApp.InitializeApp(this) returns null](https://stackoverflow.com/questions/50684222/xamarin-firebaseapp-initializeappthis-returns-null-in-60-1142-1?rq=1). It provides a new solution. – Jianwei Sun - MSFT Dec 22 '22 at 03:01

0 Answers0