gilberto
gilberto

Reputation: 157

Upgrade to lastest android/firebase sdk, this azure notification hub sample - xamarin forms

I am trying to upgrade to lastest android/firebase sdk, this sample https://github.com/xamcat/mobcat-samples/tree/master/notification_hub_backend_service

I am facing some deprecated functions and get this exception in code bellow

Java.Lang.NoSuchMethodException Message=getToken []

Very clear, but i am stucked in how to get token

 public void OnSuccess(Java.Lang.Object result)
        {
            DeviceInstallationService.Token = result.Class.GetMethod("getToken").Invoke(result).ToString();
        }

Edit: MainActivity.cs

using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Plugin.CurrentActivity;
using Xamarin.Forms;
using AppCliente.Services.LogOn;
using Android.Content;
using Microsoft.Identity.Client;
using AppCliente.Services.NotificationHub;
using AppCliente.Droid.Services;
using Firebase.Iid;
using Firebase;
using Firebase.Installations;
using Firebase.Messaging;

namespace AppCliente.Droid
{
    [Activity(Label = "AppCliente", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, Android.Gms.Tasks.IOnSuccessListener
    {
        IPushDemoNotificationActionService _notificationActionService;
        IDeviceInstallationService _deviceInstallationService;

        IPushDemoNotificationActionService NotificationActionService
            => _notificationActionService ??
                (_notificationActionService =
                ServiceContainer.Resolve<IPushDemoNotificationActionService>());

        IDeviceInstallationService DeviceInstallationService
            => _deviceInstallationService ??
                (_deviceInstallationService =
                ServiceContainer.Resolve<IDeviceInstallationService>());
        protected override void OnCreate(Bundle savedInstanceState)
        {
            CrossCurrentActivity.Current.Init(this, savedInstanceState);
            DependencyService.Register<IParentWindowLocatorService, AndroidParentWindowLocatorService>();

            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);

            Bootstrap.Begin(() => new DeviceInstallationService());

            FirebaseApp.InitializeApp(this);
            if (DeviceInstallationService.NotificationsSupported)
            {
                FirebaseInstallations.GetInstance(Firebase.FirebaseApp.Instance).GetId().AddOnSuccessListener(this);
            }

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            Syncfusion.XForms.Android.PopupLayout.SfPopupLayoutRenderer.Init();
            LoadApplication(new App());
            ProcessNotificationActions(Intent);
            App.UIParent = this;
        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }

        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(requestCode, resultCode, data);
        }

        protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
            ProcessNotificationActions(intent);
        }



        public void OnSuccess(Java.Lang.Object result)
        {
            DeviceInstallationService.Token = result.Class.GetMethod("getToken").Invoke(result).ToString();
        }
        void ProcessNotificationActions(Intent intent)
        {
            try
            {
                if (intent?.HasExtra("action") == true)
                {
                    var action = intent.GetStringExtra("action");

                    if (!string.IsNullOrEmpty(action))
                        NotificationActionService.TriggerAction(action);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }
    }
}

Upvotes: 1

Views: 292

Answers (1)

Dude named Ben
Dude named Ben

Reputation: 51

Today I've encountered that same error and fixed it

Changing this in MainActivity.cs

public void OnSuccess(Java.Lang.Object result)
{
    DeviceInstallationService.Token = result.Class.GetMethod("getToken").Invoke(result).ToString();
}

into this

public void OnSuccess(Java.Lang.Object result)
{
    DeviceInstallationService.Token = result.ToString();
}

Token is a string that was taken from the result with the GetMethod("getToken") but it seems that that's not the case anymore and the getToken method was removed.

Upvotes: 5

Related Questions