• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Coding Still

  • Home
  • About

Send FCM Notifications in C#

August 31, 2019 By _tasos 2 Comments

This post will show how one can make an API call and send a notification to an Android device.

The code below requires 2 keys for authentication, the SERVER_KEY_TOKEN and SENDER_ID can be retrieved via Firebase’s developer console. One can find those in Project Overview (click Cog icon) / Users and Permissions / Cloud Messaging tab.

The deviceId that is passed as parameter is an id of the Android phone and can be obtained via the application.

public string SendNotificationJSON(string deviceId, string title, string body, string click_action)
{
    string SERVER_KEY_TOKEN = "SERVER_KEY_TOKEN";
    var SENDER_ID = "SENDER_ID";
 
    WebRequest tRequest;
    tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
    tRequest.Method = "post";
    tRequest.ContentType = " application/json";
 
    tRequest.Headers.Add(string.Format("Authorization: key={0}", SERVER_KEY_TOKEN));
    tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
 
    var a = new
    {
        notification = new 
        {
            title,
            body,
            icon = "https://domain/path/to/logo.png",
            click_action,
            sound = "mySound"
        },
        to = deviceId
    };
 
    byte[] byteArray = Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(a));
    tRequest.ContentLength = byteArray.Length;
 
    Stream dataStream = tRequest.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();
 
    WebResponse tResponse = tRequest.GetResponse();
    dataStream = tResponse.GetResponseStream();
 
    StreamReader tReader = new StreamReader(dataStream);
    string sResponseFromServer = tReader.ReadToEnd();
 
    tReader.Close();
    dataStream.Close();
    tResponse.Close();
 
    return sResponseFromServer;
}

Additional Resources

  • This link provides a good overview on the Firebase APIs. Pretty useful to quickly find what functionality is available https://medium.com/@selvaganesh93/firebase-cloud-messaging-important-rest-apis-be79260022b5
  • This SO question has many similar yet good answers on the same topic https://stackoverflow.com/questions/37412963/send-push-to-android-by-c-sharp-using-fcm-firebase-cloud-messaging

Filed Under: .NET Development Tagged With: Android, Firebase

Reader Interactions

Comments

  1. ehsan says

    February 23, 2020 at 15:00

    1. it’s void why you have return?
    2.how can get deviceId ?
    3.if we want send to all device what we should to do?

    Reply
    • _tasos says

      March 4, 2020 at 20:00

      Hi Ehsan,

      Thank you for noticing, I updated the function’s signature.

      The device id is retrieved by the Android application, when the user enables the push notifications.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Categories

  • .NET Development
  • ASP.NET
  • Databases
  • Fun
  • IIS
  • JavaScript
  • Web Development

Tags

.NET Core Android ANTLR ASP.NET Ajax ASP.NET Core ASP.NET MVC ASP.NET Web Forms AWS Bouncy Castle Chartjs cli Client info detection Comic Continuous integration CSS Data backup Date handling Firebase Firefox addons Github HigLabo HTML5 Image manipulation jQuery JWT MySQL Nodejs Nuget OAuth Objectionjs OOP openssl Oracle ORM PHP Regular expressions SEO Social media SQL SQL Server UI/UX Url rewriting Videos Visual Studio Web design

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Secondary Sidebar

Archives

  • July 2020
  • March 2020
  • August 2019
  • December 2018
  • November 2018
  • February 2018
  • August 2016
  • June 2016
  • May 2016
  • February 2016
  • January 2016
  • August 2015
  • July 2015
  • October 2014
  • July 2014
  • November 2013
  • April 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • May 2012
  • February 2012
  • December 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010

Footer

Recent Posts

  • Anatomy of an Objection.js model
  • Check your RSA private and public keys
  • Round functions on the Nth digit
  • Send FCM Notifications in C#
  • Jwt Manager
  • Things around the web #5
  • Query JSON data as relational in MySQL
  • Create and sign JWT token with RS256 using the private key
  • Drop all database objects in Oracle
  • Create and deploy a Nuget package

Latest tweets

  • Things I Wished More Developers Knew About Databases https://t.co/h4gfq6NJgo #softwaredevelopment #databases May 3, 2020 12:52
  • How a Few Lines of Code Broke Lots of Packages https://t.co/p7ZSiLY5ca #javascript May 3, 2020 12:48
  • Can someone steal my IP address and use it as their own? https://t.co/HoQ7Z3BG69 January 24, 2020 13:27
  • Organizational complexity is the best predictor of bugs in a software module https://t.co/aUYn9hD4oa #softwaredevelopment January 13, 2020 08:24
  • Capitalism for people. Socialism for companies? https://t.co/zgUoPcqnix January 5, 2020 07:46
  • http://twitter.com/codingstill

Misc Links

  • George Liatsos Blog
  • Plethora Themes
  • C# / VB Converter
  • Higlabo: .NET library for mail, DropBox, Twitter & more

Connect with me

  • GitHub
  • LinkedIn
  • RSS
  • Twitter
  • Stack Overflow

Copyright © 2021 · eleven40 Pro on Genesis Framework · WordPress · Log in