ANGEL Canales
2 min readJul 15, 2020

--

This worked for me with version 4.3.0 Firebase.CloudMessaging

public override bool FinishedLaunching(UIApplication app, NSDictionary options)

{

global::Xamarin.Forms.Forms.Init();

Xamarin.Forms.FormsMaterial.Init();

Firebase.Core.App.Configure();

LoadApplication(application: new MyAppTest.App());// Register your app for remote notifications.

if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))

{

// iOS 10 or later

var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;

UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) => {

Console.WriteLine(granted);

});

// For iOS 10 display notification (sent via APNS)

UNUserNotificationCenter.Current.Delegate = this;

// For iOS 10 data message (sent via FCM)

Messaging.SharedInstance.Delegate = this;

Console.WriteLine($”Firebase registration token: {Messaging.SharedInstance.FcmToken}”);

}

else

{

// iOS 9 or before

var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;

var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);

UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);

}

UIApplication.SharedApplication.RegisterForRemoteNotifications();

return base.FinishedLaunching(app, options);

}

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)

{

Messaging.SharedInstance.ApnsToken = deviceToken;

//base.RegisteredForRemoteNotifications(application, deviceToken);

}

public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)

{

base.FailedToRegisterForRemoteNotifications(application, error);

}

[Export(“messaging:didReceiveMessage:”)]

public void DidReceiveMessage(Messaging messaging, RemoteMessage remoteMessage)

{

Console.WriteLine($”Firebase registration token: {Messaging.SharedInstance.FcmToken}”);

}

[Export(“messaging:didReceiveRegistrationToken:”)]

public void DidReceiveRegistrationToken(Messaging messaging, string fcmToken)

{

Console.WriteLine($”Firebase registration token: {fcmToken}”);

// TODO: If necessary send token to application server.

// Note: This callback is fired at each app startup and whenever a new token is generated.

}

public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)

{

// 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.

// TODO: Handle data of notification

// With swizzling disabled you must let Messaging know about the message, for Analytics

//Messaging.SharedInstance.AppDidReceiveMessage (userInfo);

// Print full message.

Console.WriteLine(userInfo);

}

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.

// TODO: Handle data of notification

// With swizzling disabled you must let Messaging know about the message, for Analytics

//Messaging.SharedInstance.AppDidReceiveMessage (userInfo);

// Print full message.

Console.WriteLine(userInfo);

completionHandler(UIBackgroundFetchResult.NewData);

}

// Receive displayed notifications for iOS 10 devices.

// Handle incoming notification messages while app is in the foreground.

[Export(“userNotificationCenter:willPresentNotification:withCompletionHandler:”)]

public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)

{

var userInfo = notification.Request.Content.UserInfo;

// With swizzling disabled you must let Messaging know about the message, for Analytics

//Messaging.SharedInstance.AppDidReceiveMessage (userInfo);

// Print full message.

Console.WriteLine(userInfo);

// Do something with the notification

Console.WriteLine(“Active Notification: {0}”, notification);

// Tell system to display the notification anyway or use

// `None` to say we have handled the display locally.

completionHandler(UNNotificationPresentationOptions.Alert);

}

// Handle notification messages after display notification is tapped by the user.

[Export(“userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:”)]

public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)

{

var userInfo = response.Notification.Request.Content.UserInfo;

// Print full message.

Console.WriteLine(userInfo);

completionHandler();

}

--

--