How to send Android push notifications via GCM on C# .Net

后端 未结 6 1121
礼貌的吻别
礼貌的吻别 2020-12-13 08:02

I\'m new to all Android GCM push notifications and I have read stack posts but couldn\'t get a straight answer.I have also read Create push notification in android to get a

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 08:36

    I have worked with Android Applications and IOS application using Firebase's FCM. This code is working .Middle layer is ASP.NET API.You start a project in visual studio and select API.To be create a controller.Your server_api_key and sender id adding Web.config file.

    Adding user fcm server_api_key and sender id.

    
        
        
    
    

    The below code to add your controller

     using System;
        using System.Net;
        using System.Web.Http;
        using System.Web.Script.Serialization;
        using System.Configuration;
        using System.IO;
    
        namespace pushios.Controllers
            {
    
            public class HomeController : ApiController
                {
                    [HttpGet]
    
            [Route("sendmessage")]
    
                    public IHttpActionResult SendMessage()
                  {
                        var data = new {
                            to = "Device Token",
                            data = new
                            {
                               //To be adding your json data
                                body="Test",
                                confId= "",
                                pageTitle= "test",
                                pageFormat= "",
                                dataValue= "",
                                title= "C#",
                                webviewURL= "",
                                priority = "high",
                                notificationBlastID = "0",
                                status = true
                            }
                        };
                        SendNotification(data);
                        return Ok();
                    }
                    public void SendNotification(object data)
                    {
                        var Serializer = new JavaScriptSerializer();
                        var json = Serializer.Serialize(data);
                        Byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(json);
    
                        SendNotification(byteArray);
                    }
                    public void SendNotification(Byte[] byteArray)
                    {
    
                        try
                        {
                            String server_api_key = ConfigurationManager.AppSettings["SERVER_API_KEY"];
                            String senderid = ConfigurationManager.AppSettings["SENDER_ID"];
    
                            WebRequest type = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
                            type.Method = "post";
                            type.ContentType = "application/json";
                            type.Headers.Add($"Authorization: key={server_api_key}");
                            type.Headers.Add($"Sender: id={senderid}");
    
                            type.ContentLength = byteArray.Length;
                            Stream datastream = type.GetRequestStream();
                            datastream.Write(byteArray, 0, byteArray.Length);
                            datastream.Close();
    
                            WebResponse respones = type.GetResponse();
                            datastream = respones.GetResponseStream();
                            StreamReader reader = new StreamReader(datastream);
    
                            String sresponessrever = reader.ReadToEnd();
                            reader.Close();
                            datastream.Close();
                            respones.Close();
    
                        }
                        catch (Exception)
                        {
                            throw;
                        }
    
                    }
                }
            }
    

提交回复
热议问题