Getting Started with FcmSharp and Firebase Cloud Messaging

FcmSharp is a library for sending push messages with the Firebase Cloud Messaging services. A lot of people seem to have problems getting started, so I thought it is good to show a complete end-to-end example on how to send and receive messages.

In this tutorial I assume you have followed the official documentation to create a Firebase project:

Android-side

The Firebase repositories on GitHub provide great quickstart examples for almost all use cases. For this tutorial we are going to use their Firebase Cloud Messaging Quickstart example, which is located at:

I simply cloned the entire quickstart-android repository and opened the messaging project.

Adding google-services.json to the Project

The only step, that's left to be done on the Android-side is to download the google-services.json file and add it to the project.

You start by opening the Firebase Console and going to the Project Settings of your project:

Firebase Project Settings

Then select the General Tab and click on the google-services.json download link:

Projects google-services.json

And put it in the app folder of your project. My messaging project is located at D:\github\quickstart-android\messaging, so the final link will look like this: D:\github\quickstart-android\messaging\app\google-services.json.

Additional Android Resources

If you still have problems adding Firebase to your Android application, then please consult the official Firebase documentation at:

FcmSharp-side

Downloading the Service Account Key

All messages to the Firebase Cloud Messaging API need to be signed. This requires you to first download the Service Account Key for you project.

Open the Project Settings and then go to the Service Accounts Tab. On this page click on Generate New Private Key for generating the required credentials.

Private Key for Message Signing

A warning will be shown, which reminds you to store the key securely. This is imporant, so be sure to never leak the Private Key into the public.

Download the Private Key

I have stored the Private Key to D:\serviceAccountKey.json.

Preparing the FcmSharp.Example project

I have added an example project for FcmSharp to its repository at:

In the example you need to set your Projects ID, when creating the FcmSharp Settings. To find out your Project ID, in the Firebase Console select the Project Settings and copy the Project ID in the General Tab. Then in the sample replace your_project_id with your Firebase Project ID.

// Read the Credentials from a File, which is not under Version Control:
var settings = FileBasedFcmClientSettings.CreateFromFile(@"your_project_id", @"D:\serviceAccountKey.json");

We are done with the FcmSharp-side!

Sending the Message

Getting the Device InstanceID Token

In the MainActivity.java of the Android project I set a breakpoint, where the Instance ID Token is obtained. You can also do it without a breakpoint and search the Logs for the InstanceID message:

Getting the Instance ID Token

Once you click on the LOG TOKEN Button in the sample application, the breakpoint will be hit and you can easily copy the token from the Variables pane.

Using FcmSharp to send a message to the Device

Now start the FcmSharp.Example project. A Console will open and prompt you to enter the Device Token. It is the Device Token we have just obtained from the Android application:

Sending the message with FcmSharp

Before hitting Enter, make sure to set a Breakpoint in the onMessageReceived handler of the MyFirebaseMessagingService.java class.

Receiving the Message in the Android Application

So after you have set the Breakpoint in the MyFirebaseMessagingService.java, hit enter in the FcmSharp.Example console and you should receive the message in your Android application:

Receiving the Message in Android

And that's it!