diff --git a/.gitignore b/.gitignore index f1e3d20..0c0ef0c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. +*.php + # User-specific files *.suo *.user diff --git a/Firebase-Cloud-Messaging-Net/Firebase-Cloud-Messaging-Net.csproj b/Firebase-Cloud-Messaging-Net/Firebase-Cloud-Messaging-Net.csproj index 577690c..edd82e4 100644 --- a/Firebase-Cloud-Messaging-Net/Firebase-Cloud-Messaging-Net.csproj +++ b/Firebase-Cloud-Messaging-Net/Firebase-Cloud-Messaging-Net.csproj @@ -53,9 +53,11 @@ + + diff --git a/Firebase-Cloud-Messaging-Net/Requests/Enumerations.cs b/Firebase-Cloud-Messaging-Net/Requests/Enumerations.cs index 7837c9d..736ad0f 100644 --- a/Firebase-Cloud-Messaging-Net/Requests/Enumerations.cs +++ b/Firebase-Cloud-Messaging-Net/Requests/Enumerations.cs @@ -8,10 +8,10 @@ namespace net.tipstrade.FCMNet.Requests { [JsonConverter(typeof(StringEnumConverter))] public enum Priority { /// - [EnumMember(Value = " normal")] + [EnumMember(Value = "normal")] Normal, /// - [EnumMember(Value = " high")] + [EnumMember(Value = "high")] High } } diff --git a/Firebase-Cloud-Messaging-Net/Requests/Message.cs b/Firebase-Cloud-Messaging-Net/Requests/Message.cs index c9bc4ec..950b452 100644 --- a/Firebase-Cloud-Messaging-Net/Requests/Message.cs +++ b/Firebase-Cloud-Messaging-Net/Requests/Message.cs @@ -115,14 +115,19 @@ public Message() { /// /// The Firebase Cloud Messaging authorization key. public Responses.Response Send(string key) { + var req = (HttpWebRequest)HttpWebRequest.Create(FCMSendUri); req.Method = "POST"; req.ContentType = "application/json"; - req.Headers.Add("Authorization", "key=" + key); + //req.Headers.Add("Authorization", "key=" + key); + req.Headers.Add(string.Format("Authorization: key={0}", key)); + using (var writer = new StreamWriter(req.GetRequestStream())) { + writer.NewLine = ""; #if DEBUG - var json = JsonConvert.SerializeObject(this); + var json = JsonConvert.SerializeObject(this + ); #endif writer.Write(JsonConvert.SerializeObject(this)); } diff --git a/Firebase-Cloud-Messaging-Net/Requests/Token.cs b/Firebase-Cloud-Messaging-Net/Requests/Token.cs new file mode 100644 index 0000000..142b22a --- /dev/null +++ b/Firebase-Cloud-Messaging-Net/Requests/Token.cs @@ -0,0 +1,98 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace net.tipstrade.FCMNet.Requests +{ + public class Token + { + protected const string FCMSendUri = "https://android.googleapis.com/gcm/notification"; + [JsonProperty("operation", NullValueHandling = NullValueHandling.Ignore)] + public string Operation { get; set; } + [JsonProperty("notification_key_name", NullValueHandling = NullValueHandling.Ignore)] + public string Notification_key_name { get; set; } + [JsonProperty("registration_ids", NullValueHandling = NullValueHandling.Ignore)] + public string[] Registration_ids { get; set; } + [JsonProperty("notification_key", NullValueHandling = NullValueHandling.Ignore)] + public string Notification_key { get; set; } + + #region Methods + /// + /// Sends the message. + /// + /// The Firebase Cloud Messaging authorization key. + public Responses.TokenResponse Send(string key) + { + if (this.Operation == "create") + { + this.Notification_key = null; + } + else + { + if (Notification_key == null) + throw new Exception("Notification_key must not null"); + } + + var req = (HttpWebRequest)HttpWebRequest.Create(FCMSendUri); + req.Method = "POST"; + req.ContentType = "application/json"; + //req.Headers.Add("Authorization", "key=" + key); + req.Headers.Add(string.Format("Authorization: key={0}", key)); + req.Headers.Add(string.Format("project_id:923461195943")); + + + using (var writer = new StreamWriter(req.GetRequestStream())) + { + writer.NewLine = ""; +#if DEBUG + var json = JsonConvert.SerializeObject(this + ); +#endif + writer.Write(JsonConvert.SerializeObject(this)); + } + + HttpWebResponse resp = null; + WebResponse r = null; + try + { + + //resp = (HttpWebResponse)(req.GetResponse()); + r = req.GetResponse(); + + using (var reader = new StreamReader(r.GetResponseStream())) + { + return JsonConvert.DeserializeObject(reader.ReadToEnd()); + } + + } + catch (WebException ex) + { + using (var reader = new StreamReader(ex.Response.GetResponseStream())) + { + return JsonConvert.DeserializeObject(reader.ReadToEnd()); + } + } + finally + { + if (resp != null) + resp.Dispose(); + } + } + + /// + /// Sends the message. + /// + /// The Firebase Cloud Messaging authorization key. + public async Task SendAsync(string key) + { + return await Task.Run(() => Send(key)); + } + #endregion + + } +} diff --git a/Firebase-Cloud-Messaging-Net/Responses/Response.cs b/Firebase-Cloud-Messaging-Net/Responses/Response.cs index 807ff91..12621c9 100644 --- a/Firebase-Cloud-Messaging-Net/Responses/Response.cs +++ b/Firebase-Cloud-Messaging-Net/Responses/Response.cs @@ -5,7 +5,8 @@ namespace net.tipstrade.FCMNet.Responses { /// /// Represents a Firebase Cloud Messaging message response object. /// - public class Response { + public class Response + { /// /// Gets or sets the nuber of results that contain a canonical registration token. /// diff --git a/Firebase-Cloud-Messaging-Net/Responses/TokenResponse.cs b/Firebase-Cloud-Messaging-Net/Responses/TokenResponse.cs new file mode 100644 index 0000000..04084c0 --- /dev/null +++ b/Firebase-Cloud-Messaging-Net/Responses/TokenResponse.cs @@ -0,0 +1,20 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace net.tipstrade.FCMNet.Responses +{ + public class TokenResponse + { + // ini comment + [JsonProperty("notification_key")] + public string Notification_key { get; set; } + + [JsonProperty("error")] + public string Error { get; set; } + + } +} diff --git a/Tests/Program.cs b/Tests/Program.cs index a6e6965..ca1759c 100644 --- a/Tests/Program.cs +++ b/Tests/Program.cs @@ -9,9 +9,46 @@ using net.tipstrade.FCMNet.Responses; using Newtonsoft.Json; -namespace Tests { - class Program { - static void Main(string[] args) { +namespace Tests +{ + class Program + { + static void Main(string[] args) + { + try + { + string key = "secret_key"; + Message m = new Message(); + m.To = "destination_token"; + m.Priority = Priority.High; + + Dictionary data = new Dictionary(); + data.Add("date", DateTime.Now.ToShortDateString()); + data.Add("remarks", "remarks dari .Net Console"); + data.Add("amount", "20"); + data.Add("points", "10"); + m.Data = data; + //Response r = m.Send(key); + //Console.WriteLine(r.CanonicalIDs); + + Token t = new Token(); + t.Notification_key = "" ; + t.Notification_key_name = ""; + t.Operation = "add"; + t.Registration_ids = new string[] { "" }; + Response re = t.Send(key); + + Console.Write("send Add : ", JsonConvert.SerializeObject(re)); + + t.Operation = "create"; + re = t.Send(key); + Console.Write("send Create : ",JsonConvert.SerializeObject(re)); + } + catch (Exception ex) + { + Console.WriteLine(ex.InnerException.Message); + } + + } } - } }