升级到 NET Maui 8 后,Plugin.FIrebase NotificationTapped 事件不会在前台模式下触发

问题描述 投票:0回答:1

我正在使用

Plugin.Firebase V2.0.12
来处理我的 Android 应用程序中的通知,当应用程序不处于前台模式时,它会按预期工作,但是当在前台时,
NotificationTapped
事件不会触发,我尝试了这个 MAUI:NotificationTapped 事件是未在前台模式下触发但没有成功。

值得一提的是,在将我的应用程序更新到 NET 8 之前(我使用的是 NET 7 和

Plugin.Firebase V 2.0.5
),它工作正常

这是我的代码的相关部分。

MauiProgram.cs

    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            // Initialize the .NET MAUI Community Toolkit by adding the below line of code
            .UseMauiCommunityToolkit()
            .RegisterFirebaseServices()
            .UseSkiaSharp()           
            // After initializing the .NET MAUI Community Toolkit, optionally add additional fonts
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });

        // Continue initializing your .NET MAUI App here
    }

    private static MauiAppBuilder RegisterFirebaseServices(this MauiAppBuilder builder)
    {
        builder.ConfigureLifecycleEvents(events => {
#if IOS
            events.AddiOS(iOS => iOS.FinishedLaunching((app, launchOptions) => {
                CrossFirebase.Initialize(CreateCrossFirebaseSettings());
                return false;
            }));
#else
            events.AddAndroid(android => android.OnCreate((activity, _) =>
                CrossFirebase.Initialize(activity, CreateCrossFirebaseSettings())));
#endif
        });

        //CrossFirebaseCrashlytics.Current.SetCrashlyticsCollectionEnabled(true);
        CrossFirebaseCloudMessaging.Current.TokenChanged += Current_TokenChanged;
        CrossFirebaseCloudMessaging.Current.NotificationReceived += Current_NotificationReceived;
        CrossFirebaseCloudMessaging.Current.NotificationTapped += Current_NotificationTapped;
        builder.Services.AddSingleton(_ => CrossFirebaseAuth.Current);
        return builder;
    }

    private static CrossFirebaseSettings CreateCrossFirebaseSettings()
    {
        return new CrossFirebaseSettings(isAuthEnabled: true, isCloudMessagingEnabled: true, isAnalyticsEnabled: false);
    }

    private static async void Current_NotificationTapped(object sender, Plugin.Firebase.CloudMessaging.EventArgs.FCMNotificationTappedEventArgs e)
    {
        var data = e.Notification.Data;
        var notification = new UnoNotification();
        foreach (var valuepair in data)
        {
            NotificationHelper.AssignFields(notification, valuepair.Key, valuepair.Value);

        }
        notification.ReceivedDate = notification.OrigenDate = DateTime.Now;
        if (notification.NotificationPushId <= 0) return;
        await Shell.Current.GoToAsync($"///{nameof(OrganizationsPage)}/{nameof(NotificationDetailPage)}", true,
           new Dictionary<string, object>
           {
                   {"Notification",notification },
                   {"FromNotification",true }
           });
    }

    private static void Current_NotificationReceived(object sender, Plugin.Firebase.CloudMessaging.EventArgs.FCMNotificationReceivedEventArgs e)
    {
        var notification = e.Notification;
        var data = e.Notification.Data;

#if ANDROID
        FirebaseCloudMessagingImplementation.ChannelId = "nedchat_notification_channel";
        FirebaseCloudMessagingImplementation.NotificationBuilderProvider = MainActivity.CreateNotificationBuilder;
#endif

    }


    private static void Current_TokenChanged(object sender, Plugin.Firebase.CloudMessaging.EventArgs.FCMTokenChangedEventArgs e)
    {
        var current = AppPreferencesHelper.GetFirebaseToken();
        var token = e.Token;
        if (current == token) return;
        AppPreferencesHelper.SetFirebaseToken(token);
        AppPreferencesHelper.SetIsFirebaseTokenPendingUpdate(true);
    }

我也尝试将事件放入

App.xaml.cs
但结果是一样的

平台/Android 下的

MainActivity.cs

protected override async void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    UserDialogs.Init(this);
    _context = this;
    await HandleIntent(Intent);
    CreateNotificationChannelIfNeeded();
}

 protected override async void OnNewIntent(Intent intent)
 {
     base.OnNewIntent(intent);
     await HandleIntent(intent);
 }

 private static async Task HandleIntent(Intent intent)
 {
     FirebaseCloudMessagingImplementation.OnNewIntent(intent);
     if (intent?.Extras == null) return;
     var notification = new UnoNotification();
     foreach (var key in intent.Extras.KeySet())
     {
         var value = intent.Extras.Get(key).ToString();

         NotificationHelper.AssignFields(notification, key, value);

     }
     notification.ReceivedDate = notification.OrigenDate = DateTime.Now;
     if (notification.NotificationPushId <= 0) return;

     intent.AddFlags(ActivityFlags.ClearTop);
     intent.AddFlags(ActivityFlags.SingleTop);

     await Shell.Current.GoToAsync($"///{nameof(OrganizationsPage)}/{nameof(NotificationDetailPage)}", true,
        new Dictionary<string, object>
        {
                {"Notification",notification },
                {"FromNotification",true }
        });
 }

可能出了什么问题?

c# android maui
1个回答
0
投票

所以我刚刚从我的 git 存储库中删除并重新下载了该项目,卸载然后在 Android 手机上重新安装了该应用程序,现在它工作得很好

© www.soinside.com 2019 - 2024. All rights reserved.