Firebird Cloud Message API előkészítése
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
using Google.Apis.Auth.OAuth2;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using WorkFlowCheck.BL.Services.Interfaces;
|
||||
using WorkFlowCheck.Common.DTO;
|
||||
|
||||
namespace WorkFlowCheck.API.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class FCMController : ControllerBase
|
||||
{
|
||||
private readonly IMessageService _messageService;
|
||||
public FCMController(IMessageService messageService)
|
||||
{
|
||||
_messageService = messageService;
|
||||
}
|
||||
|
||||
[HttpPost("SendFCMMessage")]
|
||||
public async Task<ApiResponseDTO<bool>> SendFCMMessage([FromBody] FCMMessageDTO fCMMessageDTO)
|
||||
{
|
||||
var retVal = new ApiResponseDTO<bool>()
|
||||
{
|
||||
IsSuccess = true
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var pathToJsonKey = "com-nuvolar-wfcapp-firebase-adminsdk-fbsvc-37642626e4.json";
|
||||
var projectId = "com-nuvolar-wfcapp";
|
||||
var credential = GoogleCredential
|
||||
.FromFile(pathToJsonKey)
|
||||
.CreateScoped("https://www.googleapis.com/auth/firebase.messaging");
|
||||
var accessToken = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync();
|
||||
|
||||
var client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
|
||||
|
||||
|
||||
var message = new
|
||||
{
|
||||
message = new
|
||||
{
|
||||
token = fCMMessageDTO.Token,
|
||||
notification = new
|
||||
{
|
||||
title = fCMMessageDTO.Title,
|
||||
body = fCMMessageDTO.Body,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var json = JsonConvert.SerializeObject(message);
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
|
||||
var response = await client.PostAsync($"https://fcm.googleapis.com/v1/projects/{projectId}/messages:send", content);
|
||||
|
||||
var result = await response.Content.ReadAsStringAsync();
|
||||
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
retVal.IsSuccess = false;
|
||||
Log.Error(ex.Message);
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
[HttpPost("SendFCMMessageToLastToken")]
|
||||
public async Task<ApiResponseDTO<bool>> SendFCMMessageToLastToken([FromBody] FCMMessageDTO fCMMessageDTO)
|
||||
{
|
||||
var retVal = new ApiResponseDTO<bool>()
|
||||
{
|
||||
IsSuccess = true
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var pathToJsonKey = "com-nuvolar-wfcapp-firebase-adminsdk-fbsvc-37642626e4.json";
|
||||
var projectId = "com-nuvolar-wfcapp";
|
||||
var credential = GoogleCredential
|
||||
.FromFile(pathToJsonKey)
|
||||
.CreateScoped("https://www.googleapis.com/auth/firebase.messaging");
|
||||
var accessToken = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync();
|
||||
|
||||
var client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
|
||||
|
||||
|
||||
var message = new
|
||||
{
|
||||
message = new
|
||||
{
|
||||
token = await _messageService.GetLastFCMTokensAsync(),
|
||||
notification = new
|
||||
{
|
||||
title = fCMMessageDTO.Title,
|
||||
body = fCMMessageDTO.Body,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var json = JsonConvert.SerializeObject(message);
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
|
||||
var response = await client.PostAsync($"https://fcm.googleapis.com/v1/projects/{projectId}/messages:send", content);
|
||||
|
||||
var result = await response.Content.ReadAsStringAsync();
|
||||
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
retVal.IsSuccess = false;
|
||||
Log.Error(ex.Message);
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
[HttpPost("SendFCMToken")]
|
||||
public async Task<ApiResponseDTO<bool>> SendFCMToken([FromBody] FCMMessageDTO fCMMessageDTO)
|
||||
{
|
||||
var retVal = new ApiResponseDTO<bool>()
|
||||
{
|
||||
IsSuccess = true
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
await _messageService.SendFCMTokenAsync(fCMMessageDTO.Token);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
retVal.IsSuccess = false;
|
||||
Log.Error(ex.Message);
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user