【发布时间】: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