Reputation: 413
I tried to implement and firebase listener in my Xamarin iOS App.
But, if my app is in foreground and an firebase cloud message receives, CrossPushNotification.Current.OnNotificationReceived is not be called.
What's problem?
[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;
}
}
Upvotes: 0
Views: 1979
Reputation: 18861
If you use the Firebase
.You should use the package Plugin.FirebasePushNotification
from NuGet
not Plugin.PushNotification
.
Change the method like following
CrossFirebasePushNotification.Current.OnTokenRefresh += (s,p) =>
{
System.Diagnostics.Debug.WriteLine($"TOKEN : {p.Token}");
};
CrossFirebasePushNotification.Current.OnNotificationReceived += (s,p) =>
{
System.Diagnostics.Debug.WriteLine("Received");
};
For more detail you can refer here
Upvotes: 1