74 lines
2.7 KiB
C#
74 lines
2.7 KiB
C#
|
|
using Microsoft.Extensions.DependencyInjection;
|
|||
|
|
using FirebaseAdmin;
|
|||
|
|
using FirebaseAdmin.Messaging;
|
|||
|
|
using Google.Apis.Auth.OAuth2;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using FileSystemData;
|
|||
|
|
using DurnyklyYol.Module.BusinessObjects;
|
|||
|
|
using DevExpress.XtraCharts;
|
|||
|
|
using System.ServiceModel.Channels;
|
|||
|
|
using DevExpress.Persistent.Base;
|
|||
|
|
|
|||
|
|
namespace DurnyklyYol.Module.Services
|
|||
|
|
{
|
|||
|
|
public class NotificationService : INotificationService
|
|||
|
|
{
|
|||
|
|
[ActivatorUtilitiesConstructor]
|
|||
|
|
public NotificationService() {
|
|||
|
|
var address = String.Format("{0}cargo-66-firebase-adminsdk.json", PathHelper.GetApplicationFolder());
|
|||
|
|
Console.WriteLine(address);
|
|||
|
|
if (FirebaseApp.DefaultInstance == null)
|
|||
|
|
FirebaseApp.Create(new AppOptions()
|
|||
|
|
{
|
|||
|
|
Credential = GoogleCredential.FromFile(address)
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
//private async Task<string> SendNotificationAsync(string title, string body, string token)
|
|||
|
|
//{
|
|||
|
|
// var message = new Message()
|
|||
|
|
// {
|
|||
|
|
// Notification = new Notification
|
|||
|
|
// {
|
|||
|
|
// Title = title,
|
|||
|
|
// Body = body
|
|||
|
|
// },
|
|||
|
|
// Token = token
|
|||
|
|
// };
|
|||
|
|
|
|||
|
|
// return await FirebaseMessaging.DefaultInstance.SendAsync(message);
|
|||
|
|
//}
|
|||
|
|
|
|||
|
|
public async Task NotifyClients(Cargo cargo)
|
|||
|
|
{
|
|||
|
|
if (cargo is not null) {
|
|||
|
|
var goodsWithClientTokens = cargo.Goods
|
|||
|
|
.Where(good => !string.IsNullOrEmpty(good.Client.FirebaseToken)) // Filter out clients with empty tokens
|
|||
|
|
.Select(good => new
|
|||
|
|
{
|
|||
|
|
GoodsCode = good.No, // Assuming 'Code' is the property of Goods
|
|||
|
|
ClientToken = good.Client.FirebaseToken
|
|||
|
|
})
|
|||
|
|
.ToList();
|
|||
|
|
if (goodsWithClientTokens.Count == 0)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
var messages = goodsWithClientTokens
|
|||
|
|
.Select(item => new FirebaseAdmin.Messaging.Message
|
|||
|
|
{
|
|||
|
|
Notification = new Notification
|
|||
|
|
{
|
|||
|
|
Title = item.GoodsCode, // Customize title for each notification if necessary
|
|||
|
|
Body = item.GoodsCode + " belgili ýüküňiz "+cargo.CurrentPoint.Name + " geldi." // Customize body for each notification if necessary
|
|||
|
|
},
|
|||
|
|
Token = item.ClientToken
|
|||
|
|
})
|
|||
|
|
.ToList();
|
|||
|
|
await FirebaseMessaging.DefaultInstance.SendEachAsync(messages);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|