public void TestTopicMessageInternalServerErrorWithouRetry() { using (var client = new FcmClient(new IntegrationTestOptions("TopicMessage_InternalServerError_WithoutRetry"))) { CancellationTokenSource cts = new CancellationTokenSource(); var message = new TopicUnicastMessage <int>(new FcmMessageOptionsBuilder().Build(), new Topic("a"), 1); bool isRetryExceptionCaught = false; bool isGeneralExceptionCaught = false; try { var result = client.SendAsync(message, cts.Token).GetAwaiter().GetResult(); } catch (FcmRetryAfterException) { isRetryExceptionCaught = true; } catch (FcmGeneralException) { isGeneralExceptionCaught = true; } Assert.AreEqual(false, isRetryExceptionCaught); Assert.AreEqual(true, isGeneralExceptionCaught); } }
public void TestTopicMessageWithRetryTimeSpan() { using (var client = new FcmClient(new IntegrationTestOptions("TopicMessage_WithRetryTimeSpan"))) { CancellationTokenSource cts = new CancellationTokenSource(); var message = new TopicUnicastMessage <int>(new FcmMessageOptionsBuilder().Build(), new Topic("a"), 1); bool isRetryExceptionCaught = false; try { var result = client.SendAsync(message, cts.Token).GetAwaiter().GetResult(); } catch (FcmRetryAfterException e) { RetryConditionValue retryConditionValue = e.RetryConditionValue; Assert.IsNotNull(retryConditionValue); Assert.IsNull(retryConditionValue.Date); // 1 Minute Delta Assert.AreEqual(retryConditionValue.Delta, TimeSpan.FromMinutes(1)); isRetryExceptionCaught = true; } Assert.AreEqual(true, isRetryExceptionCaught); } }
public void TestTopicMessageWithRetryDateTimeOffset() { using (var client = new FcmClient(new IntegrationTestOptions("TopicMessage_WithRetry"))) { CancellationTokenSource cts = new CancellationTokenSource(); var message = new TopicUnicastMessage <int>(new FcmMessageOptionsBuilder().Build(), new Topic("a"), 1); bool isRetryExceptionCaught = false; try { var result = client.SendAsync(message, cts.Token).GetAwaiter().GetResult(); } catch (FcmRetryAfterException e) { RetryConditionValue retryConditionValue = e.RetryConditionValue; Assert.IsNotNull(retryConditionValue); Assert.IsNull(retryConditionValue.Delta); // Fri, 31 Dec 1999 23:59:59 GMT Assert.AreEqual(new DateTimeOffset(1999, 12, 31, 23, 59, 59, TimeSpan.FromHours(0)), retryConditionValue.Date); isRetryExceptionCaught = true; } Assert.AreEqual(true, isRetryExceptionCaught); } }
public void TestServer() { using (var client = new FcmClient(new IntegrationTestOptions("TopicMessage_OK"))) { CancellationTokenSource cts = new CancellationTokenSource(); var message = new TopicUnicastMessage <int>(new FcmMessageOptionsBuilder().Build(), new Topic("a"), 1); var result = client.SendAsync(message, cts.Token).GetAwaiter().GetResult(); Assert.AreEqual(1234, result.MessageId); Assert.AreEqual(null, result.ErrorCode); } }
public void TestHttpClientWithProxy() { // Settings to be used: IFcmClientSettings settings = new FileBasedFcmClientSettings("/Users/bytefish/api.key"); // The Proxy Address: Uri proxyUri = new Uri(string.Format("{0}:{1}", "<proxy_address>", "<proxy_port>")); // Credentials for the Proxy: ICredentials proxyCredentials = new NetworkCredential( "<proxy_username>", "<proxy_password>" ); // Define the Proxy: IWebProxy proxy = new WebProxy { ProxyUri = proxyUri, Credentials = proxyCredentials }; // Now create a client handler with the Proxy settings: HttpClientHandler httpClientHandler = new HttpClientHandler() { Proxy = proxy, PreAuthenticate = true, UseDefaultCredentials = false, }; // Build the Custom FcmHttpClient: FcmHttpClient fcmHttpClient = new FcmHttpClient(settings, new HttpClient(httpClientHandler), JsonSerializer.Default); // Build the HttpClient: using (var client = new FcmClient(settings, fcmHttpClient)) { CancellationTokenSource cts = new CancellationTokenSource(); // Build the message: var message = new TopicUnicastMessage <int>(new FcmMessageOptionsBuilder().Build(), new Topic("a"), 1); // And send the message: var result = client.SendAsync(message, cts.Token).GetAwaiter().GetResult(); } }
public void TestUnauthorized() { using (var client = new FcmClient(new IntegrationTestOptions("TopicMessage_Unauthorized"))) { CancellationTokenSource cts = new CancellationTokenSource(); var message = new TopicUnicastMessage <int>(new FcmMessageOptionsBuilder().Build(), new Topic("a"), 1); bool isAuthenticationException = false; try { var result = client.SendAsync(message, cts.Token).GetAwaiter().GetResult(); } catch (FcmAuthenticationException) { isAuthenticationException = true; } Assert.AreEqual(true, isAuthenticationException); } }
public static void Main(string[] args) { // Read the API Key from a File, which is not under Version Control: var settings = new FileBasedFcmClientSettings("/Users/bytefish/api.key"); // Construct the Client: using (var client = new FcmClient(settings)) { // Construct the Data Payload to send: var data = new { A = new { a = 1, b = 2 }, B = 2, }; // Options for the Message: var options = FcmMessageOptions.Builder() .setTimeToLive(TimeSpan.FromDays(1)) .Build(); // The Message should be sent to the News Topic: var message = new TopicUnicastMessage <dynamic>(options, new Topic("news"), data); // Finally send the Message and wait for the Result: CancellationTokenSource cts = new CancellationTokenSource(); // Send the Message and wait synchronously: var result = client.SendAsync(message, cts.Token).GetAwaiter().GetResult(); // Print the Result to the Console: System.Console.WriteLine("Result = {0}", result); } }
public void TestAuthHeaderIsInRequest() { using (var client = new FcmClient(new IntegrationTestOptions("TopicMessage_HasAuthHeader"))) { CancellationTokenSource cts = new CancellationTokenSource(); var message = new TopicUnicastMessage <int>(new FcmMessageOptionsBuilder().Build(), new Topic("a"), 1); bool didThrow = false; try { var result = client.SendAsync(message, cts.Token).GetAwaiter().GetResult(); Assert.IsNotNull(result); } catch (Exception) { didThrow = true; } Assert.AreEqual(false, didThrow); } }
public Task <TopicMessageResponse> SendAsync <TPayload>(TopicUnicastMessage <TPayload> message, CancellationToken cancellationToken) { return(httpClient.PostAsync <TopicUnicastMessage <TPayload>, TopicMessageResponse>(message, cancellationToken)); }