crossfirebasepushnotification.current.onnotification received事件仅执行一次

发布于 2025-01-17 22:40:59 字数 5011 浏览 4 评论 0 原文

我在 Xamarin Forms 应用程序中使用带有 FCM 的推送通知,因此除了一种特定情况外,一切看起来都工作正常。

使用的包: Plugin.FirebasePushNotification

事件 CrossFirebasePushNotification.Current.OnNotificationReceived += 仅在应用程序打开和应用程序打开时调用一次开始。

如果我从服务器发送 2 个或更多通知,则仅调用第一个通知,之后停止工作。然而,无论如何,即使在前台、后台、被杀死时,通知弹出窗口总是会显示。

我希望在应用程序打开时调用它,因为我需要根据通知数据执行操作。

我正在 iOS 15.3.1 中进行测试

说明:https://github.com/CrossGeeks/FirebasePushNotificationPlugin/blob/master/docs/GettingStarted.md

版本:

"Plugin.FirebasePushNotification" Version="3.4.1"< br> "Xamarin.Forms" Version="5.0.0.2337"

提前致谢。

我的整个 AppDelegate.cs 代码:

[Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
        NSObject _onKeyboardShowObserver;
        NSObject _onKeyboardHideObserver;
       
        //
        // This method is invoked when the application has loaded and is ready to run. In this 
        // method you should instantiate the window, load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method, or iOS will terminate your application.
        //
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            Rg.Plugins.Popup.Popup.Init();
            Xamarin.FormsMaps.Init();

            Firebase.Core.App.Configure();

            global::Xamarin.Forms.Forms.Init();
            LoadApplication(new App());
            RegisterKeyBoardObserver();
            FirebasePushNotificationManager.Initialize(options, true);
            return base.FinishedLaunching(app, options);
        }
        public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken);
        }

        public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
        {
            FirebasePushNotificationManager.RemoteNotificationRegistrationFailed(error);

        }

        // To receive notifications in foreground on iOS 9 and below.
        // To receive notifications in background in any iOS version
        //[Export("application:didReceiveRemoteNotification")]
        [Export("messaging:didReceiveRemoteNotification:withCompletionHandler:")]
        public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
        {
            // If you are receiving a notification message while your app is in the background,
            // this callback will not be fired 'till the user taps on the notification launching the application.

            // If you disable method swizzling, you'll need to call this method. 
            // This lets FCM track message delivery and analytics, which is performed
            // automatically with method swizzling enabled.
            FirebasePushNotificationManager.DidReceiveMessage(userInfo);
            // Do your magic to handle the notification data
            System.Console.WriteLine(userInfo);
            
            completionHandler(UIBackgroundFetchResult.NewData);            
        }

        void RegisterKeyBoardObserver()
        {
            if (_onKeyboardShowObserver == null)
                _onKeyboardShowObserver = UIKeyboard.Notifications.ObserveWillShow((object sender, UIKeyboardEventArgs args) =>
                {
                    NSValue result = (NSValue)args.Notification.UserInfo.ObjectForKey(new NSString(UIKeyboard.FrameEndUserInfoKey));
                    CGSize keyboardSize = result.RectangleFValue.Size;
                    MessagingCenter.Send<object, KeyboardAppearEventArgs>(this, Constants.iOSKeyboardAppears, new KeyboardAppearEventArgs { KeyboardSize = (float)keyboardSize.Height });
                });
            if (_onKeyboardHideObserver == null)
                _onKeyboardHideObserver = UIKeyboard.Notifications.ObserveWillHide((object sender, UIKeyboardEventArgs args) =>
                    MessagingCenter.Send<object, string>(this, Constants.iOSKeyboardDisappears, Constants.iOSKeyboardDisappears));
        }
        
        public override void WillTerminate(UIApplication application)
        {
            if (_onKeyboardShowObserver == null)
            {
                _onKeyboardShowObserver.Dispose();
                _onKeyboardShowObserver = null;
            }

            if (_onKeyboardHideObserver == null)
            {
                _onKeyboardHideObserver.Dispose();
                _onKeyboardHideObserver = null;
            }
        }
    }

I'm using push notification with FCM in a Xamarin Forms application, so everything looks to be working fine except for one specific case.

Used package: Plugin.FirebasePushNotification

The event CrossFirebasePushNotification.Current.OnNotificationReceived += is being called only once when the application is open and when the app starts.

if I send 2 or more notifications from the server it's only called for the first notification, after that stop working. However the notification popup is always shown no matter what, even when the is in foreground, background, killed.

I want this to be called when the app is open because I need to perform an action depending on the notification data.

I'm testing in iOS 15.3.1

Intructions: https://github.com/CrossGeeks/FirebasePushNotificationPlugin/blob/master/docs/GettingStarted.md

Versions:

"Plugin.FirebasePushNotification" Version="3.4.1"
"Xamarin.Forms" Version="5.0.0.2337"

Thanks in advance.

My entire AppDelegate.cs Code:

[Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
        NSObject _onKeyboardShowObserver;
        NSObject _onKeyboardHideObserver;
       
        //
        // This method is invoked when the application has loaded and is ready to run. In this 
        // method you should instantiate the window, load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method, or iOS will terminate your application.
        //
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            Rg.Plugins.Popup.Popup.Init();
            Xamarin.FormsMaps.Init();

            Firebase.Core.App.Configure();

            global::Xamarin.Forms.Forms.Init();
            LoadApplication(new App());
            RegisterKeyBoardObserver();
            FirebasePushNotificationManager.Initialize(options, true);
            return base.FinishedLaunching(app, options);
        }
        public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken);
        }

        public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
        {
            FirebasePushNotificationManager.RemoteNotificationRegistrationFailed(error);

        }

        // To receive notifications in foreground on iOS 9 and below.
        // To receive notifications in background in any iOS version
        //[Export("application:didReceiveRemoteNotification")]
        [Export("messaging:didReceiveRemoteNotification:withCompletionHandler:")]
        public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
        {
            // If you are receiving a notification message while your app is in the background,
            // this callback will not be fired 'till the user taps on the notification launching the application.

            // If you disable method swizzling, you'll need to call this method. 
            // This lets FCM track message delivery and analytics, which is performed
            // automatically with method swizzling enabled.
            FirebasePushNotificationManager.DidReceiveMessage(userInfo);
            // Do your magic to handle the notification data
            System.Console.WriteLine(userInfo);
            
            completionHandler(UIBackgroundFetchResult.NewData);            
        }

        void RegisterKeyBoardObserver()
        {
            if (_onKeyboardShowObserver == null)
                _onKeyboardShowObserver = UIKeyboard.Notifications.ObserveWillShow((object sender, UIKeyboardEventArgs args) =>
                {
                    NSValue result = (NSValue)args.Notification.UserInfo.ObjectForKey(new NSString(UIKeyboard.FrameEndUserInfoKey));
                    CGSize keyboardSize = result.RectangleFValue.Size;
                    MessagingCenter.Send<object, KeyboardAppearEventArgs>(this, Constants.iOSKeyboardAppears, new KeyboardAppearEventArgs { KeyboardSize = (float)keyboardSize.Height });
                });
            if (_onKeyboardHideObserver == null)
                _onKeyboardHideObserver = UIKeyboard.Notifications.ObserveWillHide((object sender, UIKeyboardEventArgs args) =>
                    MessagingCenter.Send<object, string>(this, Constants.iOSKeyboardDisappears, Constants.iOSKeyboardDisappears));
        }
        
        public override void WillTerminate(UIApplication application)
        {
            if (_onKeyboardShowObserver == null)
            {
                _onKeyboardShowObserver.Dispose();
                _onKeyboardShowObserver = null;
            }

            if (_onKeyboardHideObserver == null)
            {
                _onKeyboardHideObserver.Dispose();
                _onKeyboardHideObserver = null;
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女七分熟 2025-01-24 22:40:59

一些故障排除的建议。

  1. 作为 almis 提到的

    ,将代码放入app.cs的 instart

  2. 确保初始化是正确的,请检查


如果问题仍然存在,请考虑在Github上提出问题。

Some suggestions for troubleshooting .

  1. As Almis mentioned , place the code into OnStart of App.cs.

  2. Make sure that initialization is correct, check iOS Initialization .

If problem persists , consider raising issue on github .

迷爱 2025-01-24 22:40:59

经过不同的测试,我可以找到引起问题的原因,但我不知道为什么。
我在 ontotificationReceived 事件中所做的一件事是通过调用 notificationcenter.current.current.show 方法来显示本地推送通知,这就是导致问题的原因。当我评论呼叫等待 notificationcrent.current.show(Notification)的代码行时,开始工作正常。

执行 notificationCurrent.current.show 后,CrossFireBasePushNotification插件开始工作,例如我的应用在后台或前景中。

After different tests I could find what is causing the issue, but I don’t know why.
One of the things I do in the OnNotificationReceived event is show a local Push Notification by calling the NotificationCenter.Current.Show method, and that is what is causing the problem. When I comment the line of code that calls await NotificationCenter.Current.Show(notification) start working fine.

Once the NotificationCenter.Current.Show is executed, then the CrossFirebasePushNotification plugging starts working like if my App is in background or foreground.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文