FcmSharp is a .NET library for the Firebase Cloud Messaging (FCM) API.
It implements the Firebase Cloud Messaging HTTP v1 API:
FcmSharp supports .NET Core as of Version 1.0.0.
You can use NuGet to install FcmSharp. Run the following command in the Package Manager Console.
PM> Install-Package FcmSharp
Go to the Firebase Console and choose your project. Then go to the Project Settings
(by clicking on the Gear Icon next to Project Overview
)
and select the Tab Service Accounts
. Then scroll down and select Generate Private Key
to download the JSON file. Please see the complete
example in this README for a step-by-step guide.
The Quickstart shows you how to work with FcmSharp in C#.
// Copyright (c) Philipp Wagner. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
using System.Threading;
using FcmSharp.Requests;
using FcmSharp.Settings;
namespace FcmSharp.Console
{
internal class Program
{
public static void Main(string[] args)
{
// Read the Service Account Key from a File, which is not under Version Control:
var settings = FileBasedFcmClientSettings.CreateFromFile("your_app", @"D:\serviceAccountKey.json");
// Construct the Client:
using (var client = new FcmClient(settings))
{
// Construct the Data Payload to send:
var data = new Dictionary<string, string>()
{
{"A", "B"},
{"C", "D"}
};
// The Message should be sent to the News Topic:
var message = new FcmMessage()
{
ValidateOnly = false,
Message = new Message
{
Topic = "news",
Data = data
}
};
// Finally send the Message and wait for the Result:
CancellationTokenSource cts = new CancellationTokenSource();
// Send the Message and wait synchronously:
var result = client.SendAsync(message, cts.Token).GetAwaiter().GetResult();
// Print the Result to the Console:
System.Console.WriteLine("Message ID = {0}", result.Name);
System.Console.ReadLine();
}
}
}
}
In this tutorial I assume you have followed the official documentation to create a Firebase project:
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.
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:
Then select the General Tab and click on the google-services.json download link:
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
.
If you still have problems adding Firebase to your Android application, then please consult the official Firebase documentation at:
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.
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.
I have stored the Private Key to D:\serviceAccountKey.json
.
I have added an example project for FcmSharp to its GitHub 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!
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:
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.
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:
Before hitting Enter, make sure to set a Breakpoint in the onMessageReceived
handler of the MyFirebaseMessagingService.java
class.
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:
The FcmClient
only provides an asynchronous API, and a synchronous API won't be added. I know that
asynchronous programming can be very challenging for beginners, so here is how you can turn an async
call into a synchronous one:
var result = client.SendAsync(message, cts.Token).GetAwaiter().GetResult();