Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@
<Compile Include="Requests\LocalizedNotification.cs" />
<Compile Include="Requests\Message.cs" />
<Compile Include="Requests\Notification.cs" />
<Compile Include="Requests\Token.cs" />
<Compile Include="Responses\Enumerations.cs" />
<Compile Include="Responses\Response.cs" />
<Compile Include="Responses\Result.cs" />
<Compile Include="Responses\TokenResponse.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Firebase-Cloud-Messaging-Net.nuspec" />
Expand Down
4 changes: 2 additions & 2 deletions Firebase-Cloud-Messaging-Net/Requests/Enumerations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace net.tipstrade.FCMNet.Requests {
[JsonConverter(typeof(StringEnumConverter))]
public enum Priority {
/// <summary></summary>
[EnumMember(Value = " normal")]
[EnumMember(Value = "normal")]
Normal,
/// <summary></summary>
[EnumMember(Value = " high")]
[EnumMember(Value = "high")]
High
}
}
9 changes: 7 additions & 2 deletions Firebase-Cloud-Messaging-Net/Requests/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,19 @@ public Message() {
/// </summary>
/// <param name="key">The Firebase Cloud Messaging authorization key.</param>
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));
}
Expand Down
98 changes: 98 additions & 0 deletions Firebase-Cloud-Messaging-Net/Requests/Token.cs
Original file line number Diff line number Diff line change
@@ -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
/// <summary>
/// Sends the message.
/// </summary>
/// <param name="key">The Firebase Cloud Messaging authorization key.</param>
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<Responses.TokenResponse>(reader.ReadToEnd());
}

}
catch (WebException ex)
{
using (var reader = new StreamReader(ex.Response.GetResponseStream()))
{
return JsonConvert.DeserializeObject<Responses.TokenResponse>(reader.ReadToEnd());
}
}
finally
{
if (resp != null)
resp.Dispose();
}
}

/// <summary>
/// Sends the message.
/// </summary>
/// <param name="key">The Firebase Cloud Messaging authorization key.</param>
public async Task<Responses.TokenResponse> SendAsync(string key)
{
return await Task.Run(() => Send(key));
}
#endregion

}
}
3 changes: 2 additions & 1 deletion Firebase-Cloud-Messaging-Net/Responses/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace net.tipstrade.FCMNet.Responses {
/// <summary>
/// Represents a Firebase Cloud Messaging message response object.
/// </summary>
public class Response {
public class Response
{
/// <summary>
/// Gets or sets the nuber of results that contain a canonical registration token.
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions Firebase-Cloud-Messaging-Net/Responses/TokenResponse.cs
Original file line number Diff line number Diff line change
@@ -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; }

}
}
45 changes: 41 additions & 4 deletions Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Notification> m = new Message<Notification>();
m.To = "destination_token";
m.Priority = Priority.High;

Dictionary<string, string> data = new Dictionary<string, string>();
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);
}

}
}
}
}