mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
105 lines
3.5 KiB
C#
105 lines
3.5 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
using FirebaseAdmin;
|
|
using FirebaseAdmin.Messaging;
|
|
using Google.Apis.Auth.OAuth2;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using MyCore.Framework.Business;
|
|
using MyCore.Interfaces.DTO;
|
|
using MyCore.Interfaces.Models;
|
|
using MyCore.Services;
|
|
|
|
namespace MyCore.Service.Controllers
|
|
{
|
|
//[ApiController]
|
|
[Authorize]
|
|
[Route("[controller]")]
|
|
public class NotificationController : ControllerBase
|
|
{
|
|
private readonly ILogger<NotificationController> _logger;
|
|
private HomeDatabaseService _HomeDatabaseService;
|
|
private readonly FirebaseClient _firebaseClient;
|
|
|
|
public NotificationController(ILogger<NotificationController> logger, HomeDatabaseService homeDatabaseService, FirebaseClient firebaseClient) : base()
|
|
{
|
|
_logger = logger;
|
|
this._HomeDatabaseService = homeDatabaseService;
|
|
_firebaseClient = firebaseClient;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a fcm notification
|
|
/// </summary>
|
|
/// <param name="notificationDTO">notificationDTO</param>
|
|
/// <returns>bool result</returns>
|
|
[HttpPost("all")]
|
|
[Consumes("application/json")]
|
|
public async Task<ActionResult<bool>> CreateSimpleNotification([FromBody] NotificationDTO notificationDTO)
|
|
{
|
|
try
|
|
{
|
|
if (notificationDTO.isPushNotification)
|
|
{
|
|
if (notificationDTO.notificationTitle == null
|
|
|| notificationDTO.notificationMessage == null)
|
|
return new BadRequestResult();
|
|
|
|
await NotificationLogic.PushFCMNotification(notificationDTO, null);
|
|
|
|
return Ok(true);
|
|
}
|
|
|
|
return new BadRequestResult();
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger?.LogError(ex, $"Error sending global notification to all users");
|
|
return new BadRequestResult();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a fcm notification for a specific home
|
|
/// </summary>
|
|
/// <param name="homeId">homeId</param>
|
|
/// <param name="notificationDTO">notificationDTO</param>
|
|
/// <returns>bool result</returns>
|
|
[HttpPost("home/{homeId}")]
|
|
[Consumes("application/json")]
|
|
public async Task<ActionResult<bool>> CreateSimpleNotificationForSpecificUser(string homeId, [FromBody] NotificationDTO notificationDTO)
|
|
{
|
|
try
|
|
{
|
|
if (notificationDTO.isPushNotification)
|
|
{
|
|
if (notificationDTO.notificationTitle == null
|
|
|| notificationDTO.notificationMessage == null
|
|
|| homeId == null)
|
|
return new BadRequestResult();
|
|
|
|
|
|
Home home = _HomeDatabaseService.GetById(homeId);
|
|
await NotificationLogic.PushFCMNotification(notificationDTO, home);
|
|
|
|
return Ok(true);
|
|
}
|
|
|
|
return new BadRequestResult();
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger?.LogError(ex, $"Error sending notification to specified home {homeId}");
|
|
return new BadRequestResult();
|
|
}
|
|
}
|
|
}
|
|
}
|