先修复一下,错误的场景 删除不必要的钓场资产 修复into场景 update:更新meta文件,修复报错 update:修复资源 修复第一章节建造 修复建造第三章 update:删除多余内容 update:更新README.md update:还原图标 update:更新README update:更新配置
173 lines
6.3 KiB
C#
173 lines
6.3 KiB
C#
using System.Collections.Generic;
|
|
using System;
|
|
using UnityEngine;
|
|
using Unity.Notifications;
|
|
using asap.core;
|
|
using GameCore;
|
|
using System.Linq;
|
|
using game;
|
|
using UniRx;
|
|
using Notification = Unity.Notifications.Notification;
|
|
using System.Threading.Tasks;
|
|
|
|
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
|
|
bool isInit = false;
|
|
bool initing = false;
|
|
public LocalNotificationService()
|
|
{
|
|
_notificationList = GContext.container.Resolve<cfg.Tables>().TbNotification.DataList;
|
|
}
|
|
|
|
public async System.Threading.Tasks.Task Init()
|
|
{
|
|
if (isInit) return;
|
|
#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
|
|
isInit = true;
|
|
}
|
|
|
|
public void ClearAllNotification()
|
|
{
|
|
if (!isInit) return;
|
|
rankNotifyScheduled = false;
|
|
NotificationCenter.CancelAllScheduledNotifications();
|
|
NotificationCenter.CancelAllDeliveredNotifications();
|
|
}
|
|
|
|
public void ClearNotification(int notificationID)
|
|
{
|
|
if (!IsNoticeOpen || !isInit)
|
|
{
|
|
return;
|
|
}
|
|
|
|
NotificationCenter.CancelScheduledNotification(notificationID);
|
|
NotificationCenter.CancelDeliveredNotification(notificationID);
|
|
}
|
|
|
|
public async void OpenSettings()
|
|
{
|
|
if (initing) return;
|
|
initing = true;
|
|
if (!isInit)
|
|
await Init();
|
|
else
|
|
NotificationCenter.OpenNotificationSettings();
|
|
GContext.container.Resolve<ISettingService>().ShowNotice = true;
|
|
initing = false;
|
|
}
|
|
|
|
public void ScheduleDailyNotification(int notificationID, DateTime fireTime, string title, string text)
|
|
{
|
|
if (!IsNoticeOpen || !isInit)
|
|
{
|
|
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 || !isInit) 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 || !isInit) 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 || !isInit) 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] ");
|
|
}
|
|
} |