Click here to Skip to main content
13,734,983 members
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 19 Dec 2017
Licenced CPOL

Working (19.12.2017) Push Notifications to Android with C# over Firebase Cloud Messaging

, 19 Dec 2017
Rate this:
Please Sign up or sign in to vote.
This article shows how to send push notification to your mobile (Android) app from .NET infrastructure.


Here is the full working code that shows up, how to send a push notification from .NET to your Android device(s) over new Google's Firebase Cloud (FCM) Service. All that works at 19.12.2017.


My first attempt to do such relatively simple task was a real nightmare. After registration of project on Google Firebase Console, you get a whole bunch of IDs. Searching over the internet shows "How to do that and this and somewhat", but all of them fail, just because Google changes their API and name convention very quickly - I spend really a lot of time with PHP/Perl/C# code in order to sort out, what currently works. :)

Using the Code

First Check If Notification Works

  • Go to Google Firebase Console on:
  • Login with your Google credentials
  • Check Push Notification. Also see next topic for explanation about DeviceId.

Now Write Up Three Parameters of Your Application

  • serverKey. That is a very long string (151 chars in my case) got from URL like<your project name>/settings/cloudmessaging/android:<android package name>. You can reach this from "Project Overview"->"Project Settings"->Tab "Cloud Messaging". If you don't have one, simply click on "Add Server Key". You can also have multiply keys - I don't know what the reason is, but it works (for me) with any keys. I don't also know if it works with (short) "old server key".
  • senderId. You can find this just below server keys. It is very short, and has (currently) only 12 digits
  • deviceId. That is your (also very long) app-registration id on particular device. I don't want to explain this in detail how to get this one. This one is the same as InstanceId of Registration Token associated to your App on your Device by Google Play Store Services. For explanation, see in the middle of text of this article. You don't need this one at all, if you want to notify all devices (use "/topics/all" or /topics/android etc.) or listen to particular notification and want to send particular notification (like "/topics/updateAvailable" etc.).

Now Create a WinForms C# Application

  • Create a new C# WinForms application. For such a simply thing, I don't want mess with WPF. ;)
  • Add assembly System.Web.Extensions to your assembly references collection.
  • Add Button with name btnSendNotification and Text "Send Notification" and empty TextBox with name tbxResponse. Align both on UI how you want.
  • Double-click on button btnSendNotification - it will generate an empty entry for Click-event.
  • Open file Form1.cs and put the following code:
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
using System.Windows.Forms;

namespace WindowsFormsAppNotification
    public partial class Form1 : Form
        public Form1()

        private void btnSendNotification_Click(object sender, EventArgs e)
            string response = SendPushNotification();
            tbxResponse.Text = response;

        private static string SendPushNotification()
            string response;

                // From:

                // Projekt-ID: x.y.z
                // Web-API-Key: A...Y (39 chars)
                // App-ID: 1:...:android:...

                // From
                // cloudmessaging/android:x,y,z
                // Server-Key: AAAA0...    ...._4

                string serverKey = "AAAA0...    ...._43"; // Something very long
                string senderId = "908464655627";
                string deviceId = "dj9...c:APA...    .....WTw"; // Also something very long, 
                                                                // got from android
                //string deviceId = "//topics/all";             // Use this to notify all devices, 
                                                                // but App must be subscribed to 
                                                                // topic notification
                WebRequest tRequest = WebRequest.Create("");

                tRequest.Method = "post";
                tRequest.ContentType = "application/json";
                var data = new
                    to = deviceId,
                    notification = new
                        body = "Greetings",
                        title = "Augsburg",
                        sound = "Enabled"

                var serializer = new JavaScriptSerializer();
                var json = serializer.Serialize(data);
                Byte[] byteArray = Encoding.UTF8.GetBytes(json);
                tRequest.Headers.Add(string.Format("Authorization: key={0}", serverKey));
                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();
                                response = sResponseFromServer;
            catch (Exception ex)
                response = ex.Message;

            return response;


  • Set up your parameters serverKey, senderId and deviceId in code (see above)
  • Press Send Notification
  • You should see a Response in your TextBox like this one:
  • Now, if you use "/topics/all" notification, your response will be like only:
  • Again, in order to see specific topic notification messages, your App must be registered to it. More, it must handle such notification different from Background/Foreground state and must create screen-notification by itself as response to notification.

Last Words

Android and Google Service now follows the long wanted user guideline, that they don't restart app without user interaction if app is completely stopped. So, you will probably not see a notification after "Force Stop". On some devices / Android versions, the app will be "force stopped" by wipe out from screen.

Points of Interest

Did you learn anything ...

  • zany


  • 19.12.2017 - first (and for now last) version

-- Alex --


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Germany Germany
No Biography provided

You may also be interested in...


Comments and Discussions

Questioncustom notification icon Pin
24-Jun-18 18:52
member24-Jun-18 18:52 
QuestionWhare to Add "/topics/all" to send notification for all devices ? Pin
vipan.net22-Dec-17 2:55
professionalvipan.net22-Dec-17 2:55 
AnswerRe: Whare to Add "/topics/all" to send notification for all devices ? Pin
infal23-Dec-17 15:18
memberinfal23-Dec-17 15:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06-2016 | 2.8.180920.1 | Last Updated 19 Dec 2017
Article Copyright 2017 by infal
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid