CrossPushNotification OnNotificationReceived在xamarin iOS中不被调



我试图在我的Xamarin iOS应用程序中实现和Firebase侦听器。

但是,如果我的应用位于前台并且收到 Firebase 云消息,则不会调用 CrossPushNotification.Current.OnNotificationReceived。

问题出在哪里?

[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, IUNUserNotificationCenterDelegate, IMessagingDelegate
{
public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
{
global::Xamarin.Forms.Forms.Init();
App.Configure();
this.RegisterForRemoteNotifications(launchOptions);
this.LoadApplication(new MyApp());
return base.FinishedLaunching(uiApplication, launchOptions);
}
private void RegisterForRemoteNotifications(NSDictionary launchOptions)
{
PushNotificationManager.Initialize(launchOptions, true);
CrossPushNotification.Current.RegisterForPushNotifications();
Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
CrossPushNotification.Current.OnTokenRefresh += (s, p) =>
{
System.Diagnostics.Debug.WriteLine($"TOKEN : {p.Token}");
Messaging.SharedInstance.ApnsToken = p.Token;
};
CrossPushNotification.Current.OnNotificationReceived += (s, p) =>
{
System.Diagnostics.Debug.WriteLine("Received");
};
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
PushNotificationManager.DidRegisterRemoteNotifications(deviceToken);
Messaging.SharedInstance.ApnsToken = deviceToken;
}
// To receive notifications in background in any iOS version
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
PushNotificationManager.DidReceiveMessage(userInfo);
}
public override void DidEnterBackground(UIApplication uiApplication)
{
Messaging.SharedInstance.ShouldEstablishDirectChannel = false;
}
}

如果您使用Firebase.您应该使用NuGet而不是Plugin.PushNotification中的包Plugin.FirebasePushNotification。 更改方法,如下所示

CrossFirebasePushNotification.Current.OnTokenRefresh += (s,p) =>
{
System.Diagnostics.Debug.WriteLine($"TOKEN : {p.Token}");
};
CrossFirebasePushNotification.Current.OnNotificationReceived += (s,p) =>
{
System.Diagnostics.Debug.WriteLine("Received");
};

有关更多详细信息,您可以参考此处

相关内容

  • 没有找到相关文章

最新更新