Files
back_cantanBuilding/Assets/Scripts/Services/ILocalNotificationService.cs
2026-05-26 16:15:54 +08:00

162 lines
5.9 KiB
C#

using System.Collections.Generic;
using System;
using UnityEngine;
using Unity.Notifications;
using asap.core;
using GameCore;
using System.Linq;
using cfg;
using UniRx;
using Notification = Unity.Notifications.Notification;
public interface ILocalNotificationService
{
public System.Threading.Tasks.Task Init();
public bool IsNoticeOpen { get; }
public void OpenSettings();
public void ScheduleOneTimeNotification(int notificationID, DateTime fireTime, string title, string text);
public void ScheduleDailyNotification(int notificationID, DateTime fireTime, string title, string text);
public void ClearAllNotification();
public void ClearNotification(int notificationID);
public void ScheduleDefaultNotification();
}
public class LocalNotificationService : ILocalNotificationService
{
private List<cfg.Notification> _notificationList;
#if UNITY_ANDROID
public bool IsNoticeOpen =>
Unity.Notifications.Android.AndroidNotificationCenter.UserPermissionToPost ==
Unity.Notifications.Android.PermissionStatus.Allowed;
#else
public bool IsNoticeOpen => Unity.Notifications.iOS.iOSNotificationCenter.GetNotificationSettings().AuthorizationStatus ==
Unity.Notifications.iOS.AuthorizationStatus.Authorized;
#endif
public LocalNotificationService()
{
_notificationList = GContext.container.Resolve<cfg.Tables>().TbNotification.DataList;
}
public async System.Threading.Tasks.Task Init()
{
#if UNITY_EDITOR
await System.Threading.Tasks.Task.Yield();
#else
Debug.Log("[LocalNotificationService::Init]");
var args = NotificationCenterArgs.Default;
args.AndroidChannelId = "default";
args.AndroidChannelName = "Notifications";
args.AndroidChannelDescription = "Main notifications";
NotificationCenter.Initialize(args);
Debug.Log("[LocalNotificationService::Init] Channel Inited");
await NotificationCenter.RequestPermission();
ScheduleDefaultNotification();
GContext.OnEvent<EventRankData>().Subscribe(ScheduleRankNotification);
#endif
}
public void ClearAllNotification()
{
rankNotifyScheduled = false;
NotificationCenter.CancelAllScheduledNotifications();
NotificationCenter.CancelAllDeliveredNotifications();
}
public void ClearNotification(int notificationID)
{
if (!IsNoticeOpen)
{
return;
}
NotificationCenter.CancelScheduledNotification(notificationID);
NotificationCenter.CancelDeliveredNotification(notificationID);
}
public void OpenSettings()
{
NotificationCenter.OpenNotificationSettings();
}
public void ScheduleDailyNotification(int notificationID, DateTime fireTime, string title, string text)
{
if (!IsNoticeOpen)
{
Debug.Log("[LocalNotificationService::ScheduleDailyNotification] Not Open");
return;
}
NotificationCenter.CancelScheduledNotification(notificationID);
NotificationCenter.CancelDeliveredNotification(notificationID);
var notification = new Notification()
{
Title = title,
Text = text,
Identifier = notificationID
};
NotificationCenter.ScheduleNotification(notification,
new NotificationDateTimeSchedule(fireTime, NotificationRepeatInterval.Daily));
}
public void ScheduleOneTimeNotification(int notificationID, DateTime fireTime, string title, string text)
{
if (!IsNoticeOpen) return;
NotificationCenter.CancelScheduledNotification(notificationID);
NotificationCenter.CancelDeliveredNotification(notificationID);
var notification = new Notification()
{
Title = title,
Text = text,
Identifier = notificationID
};
NotificationCenter.ScheduleNotification(notification,
new NotificationDateTimeSchedule(fireTime, NotificationRepeatInterval.OneTime));
}
public void ScheduleDefaultNotification()
{
if (!IsNoticeOpen) return;
Debug.Log("[LocalNotificationService::SendNotification] Daily");
var dailyNotification = _notificationList.Where(x => x.Type == 0);
foreach (var item in dailyNotification)
{
var dateTime = DateTime.SpecifyKind(DateTime.Parse(item.NotTime), DateTimeKind.Utc);
ScheduleDailyNotification(item.ID, dateTime.ToLocalTime(), LocalizationMgr.GetText(item.Title_l10n_key),
LocalizationMgr.GetText(item.Content_l10n_key));
}
Debug.Log("[LocalNotificationService::SendNotification] Daily Done");
}
private bool rankNotifyScheduled = false;
private void ScheduleRankNotification(EventRankData evt)
{
if (rankNotifyScheduled || !IsNoticeOpen) return;
Debug.Log("[LocalNotificationService::SendNotification] RankEvent");
var notification = _notificationList.FirstOrDefault(x => x.Type == 1);
if (notification == null) return;
// var fireTime = ConvertTools.GetDateTimeYMD(ZZTimeHelper.UtcNow().UtcNowOffset());
// var dayTime = DateTime.Parse(notification.NotTime);
// fireTime = fireTime.AddDays(1).AddHours(dayTime.Hour).AddMinutes(dayTime.Minute).AddSeconds(dayTime.Second);
var now = ZZTimeHelper.UtcNow();
var fireTime = DateTime.SpecifyKind(DateTime.Parse(notification.NotTime), DateTimeKind.Utc);
fireTime = fireTime.AddDays(now <= fireTime ? 0 : 1);
Debug.Log($"<color=#00d6b9>Try schedule Rank Notification at {fireTime}.</color>");
ScheduleOneTimeNotification(notification.ID, fireTime.ToLocalTime(),
LocalizationMgr.GetText(notification.Title_l10n_key),
LocalizationMgr.GetText(notification.Content_l10n_key));
rankNotifyScheduled = true;
Debug.Log("[LocalNotificationService::SendRankEventNotification] ");
}
}