Sample Code for sending Firebase Cloud Message (FCM) with Xamarin


This blog show how to setup Firebase Cloud Message for Xamarin.Android

First, register project at

Package name at firebase console

Make sure the package is your xamarin app's package ID, for example, com.yourcompany.yourapp.

Also, make note at for following values, because you'll need those when you add code at Xamarin.Android.

  • Project ID
    • Firebase Setting, General Tab.
  • Web API Key
    • Firebase Setting, General Tab
  • Sender ID
    • Firebase Setting, Cloud MessagingTab


First, following example code is under these version.

  • Visual Studio 2015 - Version 14 Update 3
  • Xamain -
  • Xamarin.Android -
  • Xamarin.iOS-

Add code to Xamarin.Android Code

packages.config at Xamarin.Android

We'll need firebase and google play service.

  <package id="Xamarin.Firebase.Common" version="42.1021.0" targetFramework="monoandroid71" />
  <package id="Xamarin.Firebase.Iid" version="42.1021.0" targetFramework="monoandroid71" />
  <package id="Xamarin.Firebase.Messaging" version="42.1021.0" targetFramework="monoandroid71" />
  <package id="Xamarin.Forms" version="" targetFramework="monoandroid71" />
  <package id="Xamarin.GooglePlayServices.Basement" version="42.1021.0" targetFramework="monoandroid71" />
  <package id="Xamarin.GooglePlayServices.Tasks" version="42.1021.0" targetFramework="monoandroid71" />


<application android:label="YourAppLabel" android:icon="@drawable/icon">
    <meta-data android:name="" android:value="@integer/google_play_services_version" />
      android:exported="false" />

      android:permission="" >
        <action android:name="" />
        <action android:name="" />
        <category android:name="${applicationId}" />

  <uses-permission android:name="" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />
  <uses-permission android:name="android.permission.GET_ACCOUNTS" />
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


Based on the current document, need google-services.json under project. You could get this under, however I'm still not sure why do we need this file. I'll do more research for this later.

Add code to MainActivity

  private void FirebaseNotificationSetup()
            var options = new FirebaseOptions.Builder()

            var firebaseApp = FirebaseApp.InitializeApp(ApplicationContext, options, APP_NAME);
            if (FirebaseApp.GetApps(this) != null)
                var x = FirebaseApp.GetInstance(APP_NAME);

            Task.Run(() =>
                var instanceID = FirebaseInstanceId.Instance;
                var iid1 = instanceID.Token;
                var iid2 = instanceID.GetToken(GetConfigValues(ConfigKeys.FCMAndroidSenderID), 

                // subscribe the topic as Test
                // this will be /topics/test
        public enum ConfigKeys

        public static string GetConfigValues(ConfigKeys keys)
            switch (keys)
                case ConfigKeys.FCMAndroidProjectID:
                    return GetValue("YourProjectIdForDebug", "YourProjectIdForRelease");
                case ConfigKeys.FCMAndroidSenderID:
                    return GetValue("YourSenderIDForDebug", "YourSenderIDForRELEASE");
                case ConfigKeys.FCMAndroidAPIKEY:
                    return GetValue("YourWebAPIKEYForDebug", "YourWebAPIKEYForRELEASE");
                    return string.Empty;

        public static string GetValue(string debugValue, string releaseValue)
            return debugValue;
            return releaseValue;
            return string.Empty;

Code for refresh token

    [IntentFilter(new[] { "" })]
    public class MyFirebaseIIDService : FirebaseInstanceIdService
        const string TAG = "MyFirebaseIIDService";
        public override void OnTokenRefresh()
            var refreshedToken = FirebaseInstanceId.Instance.Token;
        void SendRegistrationToServer(string token)
            // Add custom implementation, as needed.

Code for receive message from app server

    [IntentFilter(new[] { "" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
        const string TAG = "MyFirebaseMsgService";

        public override void OnMessageReceived(RemoteMessage message)

        void SendNotification(string messageBody)
            var intent = new Intent(this, typeof(MainActivity));
            var pendingIntent = PendingIntent.GetActivity(this, 0 /* Request code */, intent, PendingIntentFlags.OneShot);

            var defaultSoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
            var notificationBuilder = new NotificationCompat.Builder(this)
                .SetContentTitle("FCM Message")

            var notificationManager = NotificationManager.FromContext(this);

            notificationManager.Notify(0, notificationBuilder.Build());

App server code example


                string applicationID = " cloud message server key";

                string senderId = "sender ID";

                string deviceId = "device created by your device, emulator or actual device";

                WebRequest tRequest = WebRequest.Create("");
                tRequest.Method = "post";
                tRequest.ContentType = "application/json";
                var data = new
                    // send to device
                    //to = deviceId,
   // send to subscribed topic "test"                 
   to = "/topics/Test",

                    notification = new
                        body = "body",
                        title = "my title",
                        sound = "Enabled"


                var json = JsonConvert.SerializeObject(data);
                Byte[] byteArray = Encoding.UTF8.GetBytes(json);
                tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
                tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
                tRequest.ContentLength = byteArray.Length;
                using (Stream dataStream = tRequest.GetRequestStream())
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    using (WebResponse tResponse = tRequest.GetResponse())
                        using (Stream dataStreamResponse = tResponse.GetResponseStream())
                            using (StreamReader tReader = new StreamReader(dataStreamResponse))
                                String sResponseFromServer = tReader.ReadToEnd();
                                string str = sResponseFromServer;
            catch (Exception ex)
                string str = ex.Message;

That's it

You could either use above app server code to send message or go to Firebase UI to send message to your device.

What's more

How to change project ID, sender ID for different Enviroment and since have to include google-services.json to the project?

Things for watch out

the topic should be match this format [a-zA-Z0-9-_.~%]{1,900}, otherwise will get exceptin error.

{Java.Lang.IllegalArgumentException: Invalid topic name: