Register for remote notifications
Either at startup, or at the desired point in your application flow, register your app for remote notifications. Call RegisterForRemoteNotifications
method as shown in FinishedLaunching method:
if (UIDevice.CurrentDevice.CheckSystemVersion (10, 0)) {
var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
UNUserNotificationCenter.Current.RequestAuthorization (authOptions, (granted, error) => {
Console.WriteLine (granted);
});
UNUserNotificationCenter.Current.Delegate = this;
Messaging.SharedInstance.RemoteMessageDelegate = this;
} else {
var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
var settings = UIUserNotificationSettings.GetSettingsForTypes (allNotificationTypes, null);
UIApplication.SharedApplication.RegisterUserNotificationSettings (settings);
}
Firebase.Analytics.App.Configure();
UIApplication.SharedApplication.RegisterForRemoteNotifications ();
Firebase.InstanceID.InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
{
var newToken = Firebase.InstanceID.InstanceId.SharedInstance.Token;
// if you want to send notification per user, use this token
System.Diagnostics.Debug.WriteLine(newToken);
});
Receive messages
Firebase notifications behave differently depending on the foreground/background state of the receiving app.
Receive messages through FCM
To receive or send messages through FCM, you'll need to connect to the FCM service. Connect when your application becomes active and whenever a new registration token is available.
Messaging.SharedInstance.Connect (error => {
if (error != null) {
} else {
}
});
When your app goes into the background, disconnect from FCM:
public override void DidEnterBackground (UIApplication application)
{
Messaging.SharedInstance.Disconnect ();
Console.WriteLine ("Disconnected from FCM");
}
Handling messages
For devices running iOS 9 and below, override AppDelegate's DidReceiveRemoteNotification
method to handle notifications received when the client app is in the foreground, and all data messages that are sent to the client. The message is a dictionary of keys and values:
public override void DidReceiveRemoteNotification (UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
System.Console.WriteLine (userInfo);
}
For iOS 10 devices and above, implement IUNUserNotificationCenterDelegate
interface and override WillPresentNotification
method to handle notifications received when the client app is in the foreground:
// To receive notifications in foreground on iOS 10 devices.
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
// Do your magic to handle the notification data
System.Console.WriteLine(notification.Request.Content.UserInfo);
}
public void ApplicationReceivedRemoteMessage(RemoteMessage remoteMessage)
{
Console.WriteLine(remoteMessage.AppData);
}
How to Send a message to a single device.
Access the registration token
To send a message to a specific device, you need to know that device's registration token.
To retrieve the current token, you will need to call the following property. This property returns null if the token has not yet been generated:
var token = InstanceId.SharedInstance.Token;
Send a message
- Install and run the app on the target device. You'll need to accept the request for permission to receive remote notifications.
- Make sure the app is in the background on the device.
- Open the Notifications tab of the Firebase console and select New Message.
- Enter the message text.
- Select Single Device for the message target.
- In the field labeled FCM Registration Token, enter the registration token you obtained in a previous section of this guide.
Reference-