AcaMate_API/Program/V1/Controllers/PushController.cs

114 lines
3.7 KiB
C#

using AcaMate.Common.Models;
using AcaMate.V1.Models;
using Microsoft.AspNetCore.Mvc;
using AcaMate.V1.Services;
namespace AcaMate.V1.Controllers;
[ApiController]
[Route("/api/v1/in/push")]
[ApiExplorerSettings(GroupName = "공통")]
public class PushController : ControllerBase
{
private readonly IWebHostEnvironment _env;
public PushController(IWebHostEnvironment env)
{
_env = env;
}
[HttpGet()]
[CustomOperation("푸시 확인", "저장된 양식을 확인 할 수 있다..", "푸시")]
public IActionResult GetPushData()
{
return Ok("SEND");
}
/// <summary>
/// Sends a push notification to the specified device token with the provided payload.
/// </summary>
/// <param name="deviceToken">The device token to send the notification to.</param>
/// <param name="payload">The payload of the push notification.</param>
/// <returns>An IActionResult indicating the result of the operation.</returns>
/// <response code="200">Push notification sent successfully.</response>
/// <response code="400">Invalid input parameters.</response>
/// <response code="500">Internal server error occurred.</response>
/// <response code="999">Service unavailable.</response>
[HttpPost("send")]
[CustomOperation("푸시전송", "저장된 양식으로, 사용자에게 푸시를 전송한다.(로컬 테스트 불가)", "푸시")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(APIResponseStatus<object>))]
public async Task<IActionResult> SendPush(string deviceToken, [FromBody] Payload? payload)
{
if (string.IsNullOrWhiteSpace(deviceToken) || payload == null)
return BadRequest(APIResponse.InvalidInputError());
// string uri = "";
// string p12Path = "";
//
// string p12PWPath = "/src/private/appleKeys.json";
//
// // 앱 번들 ID 입력 부분
// string apnsTopic = "me.myds.ipstein.acamate.AcaMate";
Func<bool,string,string, ApnsPushService> pushService = (isDevelopment,pw,topic) =>
{
if (isDevelopment)
{
return new ApnsPushService(
"https://api.sandbox.push.apple.com/",
"/src/private/AM_Push_Sandbox.p12",
pw, topic
);
}
else
{
return new ApnsPushService(
"https://api.push.apple.com/",
"/src/private/AM_Push.p12",
pw, topic
);
}
};
//
// if (_env.IsDevelopment())
// {
// uri = "https://api.sandbox.push.apple.com/";
// p12Path = "/src/private/AM_Push_Sandbox.p12";
// }
// else
// {
// uri = "https://api.push.apple.com/";
// p12Path = "/src/private/AM_Push.p12";
// }
//
// ApnsPushService 인스턴스 생성
// var pushService = new ApnsPushService(uri, p12Path, p12PWPath, apnsTopic);
//
// 푸시 알림 전송
// var result = await pushService.SendPushNotificationAsync(deviceToken, payload);
var result = await pushService(
_env.IsDevelopment(),
"/src/private/appleKeys.json",
"me.myds.ipstein.acamate.AcaMate"
).SendPushNotificationAsync(deviceToken, payload);
if (result.Success)
{
return Ok(APIResponse.Success());
}
else
{
return BadRequest(APIResponse.InternalSeverError());
}
}
}