Skip to main content

send push notification from C# using FCM

        public bool SendPushNotification(string deviceId, string message)
        {
     //applicationID = "AIzaSyxxxxxxxxxxxxxxxxxxx"
    // senderId = "98xxxxxxxxx"
            bool isSent = false;
            string str = "";
            try
            {

                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 = message,
                        title = "SpAlert",
                        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}", utilityHelper.PushNotificationAppId));
                tRequest.Headers.Add(string.Format("Sender: id={0}", utilityHelper.PushNotificationSenderID));
                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();
                                str = sResponseFromServer;
                                Console.WriteLine(sResponseFromServer);
                                isSent = true;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                str = ex.Message;
            }
            return isSent;
        }

Comments

Popular posts from this blog

Create Strong Name Assembly

From a VS.NET command prompt, enter the following: 1. Generate a KeyFile sn -k keyPair.snk 2. Get the MSIL for the assembly ildasm SomeAssembly.dll /out:SomeAssembly.il 3. Rename the original assembly, just in case ren SomeAssembly.dll SomeAssembly.dll.orig 4. Build a new assembly from the MSIL output and your KeyFile ilasm SomeAssembly.il /dll /key= keyPair.snk   more read visit : http://www.geekzilla.co.uk/ViewCE64BEF3-51A6-4F1C-90C9-6A76B015C9FB.htm

Sql Helper into Xamarin Forms PCL Project

public abstract class SQLiteConnection : IDisposable { public string DatabasePath { get ; private set ; } public bool TimeExecution { get ; set ; } public bool Trace { get ; set ; } public SQLiteConnection ( string databasePath) { DatabasePath = databasePath; } public abstract int CreateTable < T > (); public abstract SQLiteCommand CreateCommand ( string cmdText, params object [] ps); public abstract int Execute ( string query, params object [] args); public abstract List < T > Query < T > ( string query, params object [] args) where T : new (); public abstract TableQuery < T > Table < T > () where T : new (); public abstract T Get < T > ( object pk) where T : new (); public bool IsInTransaction { get ; protected set ; } public abstract void BeginTransaction (); public abstract void Rollback (); public abstract void Co

Difference between Abstract Class and Interface with example in c#

1. Multiple inheritance  A class may inherit several interfaces.  class abc : ICrud,IDisposal  A class may inherit only one abstract class.  class abc : parentClass 2. Default implementation Interface : An interface cannot provide any code, just the signature. Example: interface IPerson     {         void Add(string name, string email);     }     Abstract class : An abstract class can provide complete, default code and/or just the details that have to be overridden. Example: abstract class APerson     {         //         string Name;         string Email;        // like interface        public abstract void Add(string name, string email);               public void Update()        {        }     } 3. Access Modifiers Interface : An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public     interface IPerson     {         void Add(string name, string email);     }     class iPerson :