crossfirebasepushnotification.current.onnotification received事件仅执行一次
我在 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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些故障排除的建议。
作为
almis
提到的,将代码放入app.cs的 instart
确保初始化是正确的,请检查。
如果问题仍然存在,请考虑在Github上提出问题。
Some suggestions for troubleshooting .
As
Almis
mentioned , place the code intoOnStart
of App.cs.Make sure that initialization is correct, check iOS Initialization .
If problem persists , consider raising issue on github .
经过不同的测试,我可以找到引起问题的原因,但我不知道为什么。
我在
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 theNotificationCenter.Current.Show
method, and that is what is causing the problem. When I comment the line of code that calls awaitNotificationCenter.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.