46 lines
1.6 KiB
C#

using FirebaseAdmin.Messaging;
using MyCore.Interfaces.DTO;
using MyCore.Interfaces.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.Framework.Business
{
public class NotificationLogic
{
public static async Task<bool> PushFCMNotification(NotificationDTO notificationDTO, Home home)
{
try
{
// Push FCM notif to phones
var message = new FirebaseAdmin.Messaging.Message()
{
Topic = home == null ? "main" : home.Id,
Notification = new Notification()
{
Title = notificationDTO.notificationTitle,
Body = home == null ? notificationDTO.notificationTitle : $"{home.Name}, {notificationDTO.notificationMessage}"
},
Data = new Dictionary<string, string>()
{
{ "labelButton", notificationDTO.notificationLabelButton },
{ "type", notificationDTO.type },
},
};
// Send a message to the device corresponding to the provided
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);
return true;
}
catch (Exception e)
{
return false;
}
}
}
}