Click here to Skip to main content
15,891,316 members
Articles / Mobile Apps / Android
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
19 Dec 2017CPOL3 min read 21.4K   9   3
This article shows how to send push notification to your mobile (Android) app from .NET infrastructure.

Introduction

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.

Background

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: https://console.firebase.google.com
  • 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 https://console.firebase.google.com/project/<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:
C#
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()
        {
            InitializeComponent();
        }

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

        private static string SendPushNotification()
        {
            string response;

            try
            {
                // From: https://console.firebase.google.com/project/x.y.z/settings/general/android:x.y.z

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

                // From https://console.firebase.google.com/project/x.y.z/settings/
                // 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("https://fcm.googleapis.com/fcm/send");

                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;
        }
    }
}

Finally

  • 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:
    JavaScript
    {"multicast_id":7245675637858457127,"success":1,"failure":0,"canonical_ids":0,
    "results":[{"message_id":"0:1513640171377700%6265d9e96265d9e9"}]}
  • Now, if you use "/topics/all" notification, your response will be like only:
    JavaScript
    {"message_id":6032969957214653029}
  • 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

History

  • 19.12.2017 - first (and for now last) version

-- Alex --

License

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


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questioncustom notification icon Pin
Member 1348386324-Jun-18 18:52
Member 1348386324-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 
Thanks for this. Smile | :) Thumbs Up | :thumbsup:
Could you please let me know where we can us "/topics/all" D'Oh! | :doh: for send notification to all device ?
If possible, Please provide me sample code.
Your help is highly appreciable.
Waiting for your revert. Smile | :)
AnswerRe: Whare to Add "/topics/all" to send notification for all devices ? Pin
infal23-Dec-17 15:18
infal23-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.