-
Notifications
You must be signed in to change notification settings - Fork 0
/
FcmAPIClient.cs
113 lines (90 loc) · 3.8 KB
/
FcmAPIClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using FcmSharp.MessageEntities.Request;
using FcmSharp.MessageEntities.Response;
using Jil;
using Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
namespace FcmSharp
{
public class FcmAPIClient
{
string apiKey;
string baseUri;
public FcmAPIClient(string apiKey, string baseUri = "https://fcm.googleapis.com/fcm/send")
{
this.apiKey = apiKey;
this.baseUri = baseUri;
}
/// <summary>
///
/// </summary>
/// <param name="options"></param>
/// <param name="to" <see cref="Message.To"/>
/// <param name="data"></param>
public async Task<MessageResponse> SendAsync<TResult>(MessageOptions options, string to, object data, NotificationPayload notification = null) where TResult : MessageResponse
{
string response = await SendAsync(options, to, data, notification);
return JSON.Deserialize<MessageResponse>(response, Options.CamelCase);
}
public async Task<MessageResponse> SendAsync<TResult>(MessageOptions options, List<string> registrationdIds, object data, NotificationPayload notification = null) where TResult : MessageResponse
{
string response = await SendAsync(options, registrationdIds, data, notification);
return JSON.Deserialize<MessageResponse>(response, Options.CamelCase);
}
public async Task<string> SendAsync(MessageOptions options, string to, object data, NotificationPayload notification = null)
{
DataMessage dataMessage = new DataMessage(options, to, data, notification);
return await PostAsync(dataMessage);
}
public async Task<string> SendAsync(MessageOptions options, List<string> registrationdIds, object data, NotificationPayload notification = null)
{
DataMessage dataMessage = new DataMessage(options, registrationdIds, data, notification);
return await PostAsync(dataMessage);
}
private async Task<string> PostAsync(DataMessage requestMessage)
{
WebRequest wRequest = WebRequest.Create(baseUri);
wRequest.ContentType = "application/json";
wRequest.Method = WebRequestMethods.Http.Post;
wRequest.Headers.Add($"Authorization:key={apiKey}");
using (var reqStream = await wRequest.GetRequestStreamAsync())
using (var streamWriter = new StreamWriter(reqStream))
{
string json = JSON.SerializeDynamic(requestMessage, Options.IncludeInherited);
await streamWriter.WriteAsync(json);
}
var retryStrategy = new ExponentialBackoff(3, TimeSpan.FromSeconds(2),
TimeSpan.FromSeconds(20), TimeSpan.FromSeconds(1));
var retryPolicy = new RetryPolicy<WebExceptionDetectionStrategy>(retryStrategy);
return await retryPolicy.ExecuteAction(async () =>
{
using (var httpResponse = await wRequest.GetResponseAsync())
using (var responseStream = httpResponse.GetResponseStream())
using (var streamReader = new StreamReader(responseStream))
{
return await streamReader.ReadToEndAsync();
}
});
}
}
class WebExceptionDetectionStrategy : ITransientErrorDetectionStrategy
{
public bool IsTransient(Exception ex)
{
WebException wException = ex as WebException;
if (wException != null)
{
var response = wException.Response as HttpWebResponse;
if (response != null)
{
HttpStatusCode statusCode = response.StatusCode;
return statusCode == HttpStatusCode.GatewayTimeout || statusCode == HttpStatusCode.RequestTimeout || statusCode == HttpStatusCode.ServiceUnavailable;
}
}
return false;
}
}
}