【问题标题】:CrossFirebasePushNotification.Current.OnNotificationReceived event is only executed onceCrossFirebasePushNotification.Current.OnNotificationReceived 事件只执行一次
【发布时间】:2022-07-06 23:08:16
【问题描述】:

我在 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"
"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;
            }
        }
    }

【问题讨论】:

  • 您在哪里订阅事件(CrossFirebasePushNotification.Current.OnNotificationReceived += 部分在哪里)。它应该在您的 App.cs 中的 protected override void OnStart
  • 是的,它在protected override void OnStart 方法中。感谢您的澄清。

标签: c# xamarin push-notification firebase-cloud-messaging


【解决方案1】:

一些故障排除建议。

  1. Almis 所述,将代码放入App.cs 的OnStart

  2. 确保初始化正确,检查iOS Initialization

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

【讨论】:

  • 检查 iOS 初始化我发现 FirebaseAppDelegateProxyEnabled 属性的类型设置为字符串,它应该是布尔值并再次测试但没有成功。\\这里收到通知的日志: WillPresentNotification: Notification request not foundUNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called.
  • 另外,关于你的第三点:如果OnNotificationReceived应该在我们点击通知后触发,为什么它会在第一个通知到达时触发?他们还有一个名为OnNotificationOpened 的活动。 @ColeX 感谢您的回答和建议
  • @FranklinFrias 你是对的 OnNotificationReceived 应该在每次收到通知时触发。有些人抱怨版本 13.5.xx,也许尝试在旧版本上测试它,看看是否是问题。
  • @FranklinFrias 你是对的,OnNotificationReceived 应该在收到通知时触发。如果可能的话,您能否在 AppDelegate.cs 中提供整个代码?
  • @ColeX-MSFT 经过不同的测试,我可以找到导致问题的原因,但我不知道为什么。我在OnNotificationReceived 事件中所做的一件事是通过调用NotificationCenter.Current.Show 方法显示本地推送通知,这就是导致问题的原因。当我注释调用await NotificationCenter.Current.Show(notification) 的代码行时开始正常工作。
【解决方案2】:

经过不同的测试,我可以找到导致问题的原因,但我不知道为什么。 我在OnNotificationReceived 事件中所做的一件事是通过调用NotificationCenter.Current.Show 方法显示本地推送通知,这就是导致问题的原因。当我注释调用 await NotificationCenter.Current.Show(notification) 的代码行时开始正常工作。

一旦NotificationCenter.Current.Show 被执行,CrossFirebasePushNotification 插件就会开始工作,就像我的应用程序在后台或前台一样。

【讨论】:

    猜你喜欢
    • 2022-01-07
    • 2017-11-24
    • 1970-01-01
    • 2012-06-09
    • 2014-05-23
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多