Created
February 29, 2020 07:34
-
-
Save jackyli-work/11f3856891f42a9dd0a154800df8ca08 to your computer and use it in GitHub Desktop.
LocalNotification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Assets.SimpleAndroidNotifications; | |
using JFun.Foundation.Loggers; | |
using JFun.Framework; | |
using UnityEngine; | |
namespace JFun.Gameplay.CreateWorld | |
{ | |
#if UNITY_ANDROID || UNITY_EDITOR | |
public class AndroidLocalNotification : ILocalNotification | |
{ | |
private Color _smallIconColor = new Color(0.984f, 0.321f, 0.05f); | |
private readonly TimeSpan _perOneMin = TimeSpan.FromSeconds(60); | |
private readonly TimeSpan _perDay = TimeSpan.FromDays(1); | |
private readonly TimeSpan _perTwoHour = TimeSpan.FromHours(2); | |
private readonly TimeSpan _perFourHour = TimeSpan.FromHours(4); | |
private readonly TimeSpan _perEightHour = TimeSpan.FromHours(8); | |
private const string _keyNotis = "Android_LocalNotis_Ids"; | |
private const string _keyNotisDisabled = "Android_LocalNotis_Ids_Disabled"; | |
private Dictionary<int, LocalNotiParams> _notis = new Dictionary<int, LocalNotiParams>(); | |
private Dictionary<int, bool> _notiActives = new Dictionary<int, bool>(); | |
public AndroidLocalNotification(IAppPause iAppPause) | |
{ | |
iAppPause.OnAppPauseEvent += OnAppPause; | |
} | |
private void OnAppPause(bool pause) | |
{ | |
//OnBackGround | |
if (pause) | |
{ | |
SaveNotis(_keyNotis, _notis); | |
SaveDisabledIds(); | |
LocalNotiParamsSaver.Execute(); | |
} | |
//On ForeGround | |
else | |
{ | |
LoadNotisWithoutExpried(_keyNotis, _notis); | |
LoadDisabledIds(); | |
} | |
} | |
public void SetActive(LocalNotiKey key, bool isActive) | |
{ | |
SetActive((int)key, isActive); | |
} | |
public void SetActive(int id, bool isActive) | |
{ | |
_notiActives[id] = isActive; | |
if (_notis.ContainsKey(id)) | |
{ | |
if (isActive) | |
{ | |
Send(_notis[id], id); | |
} | |
else | |
{ | |
ShareLogger.Instance.Debug("[AndroidLocalNotification][SetActive] id {0} , Cancel This Noti , Becase This Noti SetActive False.", id); | |
Cancel(id, false); | |
} | |
} | |
} | |
public bool IsPendingNoti(LocalNotiKey key) | |
{ | |
return IsPendingNoti((int)key); | |
} | |
public bool IsPendingNoti(int id) | |
{ | |
if (!_notis.ContainsKey(id)) | |
return false; | |
return _notis[id].FireDate > DateTime.Now; | |
} | |
public void Send(LocalNotiKey key, LocalNotiParams @params) | |
{ | |
Send(@params, (int)key); | |
} | |
public void Cancel(LocalNotiKey key) | |
{ | |
Cancel((int)key); | |
} | |
public int Send(LocalNotiParams @params) | |
{ | |
return Send(@params, RandId()); | |
} | |
public int Send(LocalNotiParams @params, int key) | |
{ | |
if (@params.FireDate < DateTime.Now) | |
{ | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Warn) | |
ShareLogger.Instance.Warn("[AndroidLocalNotification][Send] FireDate < DateTime Now . You Can't See This Notifaction"); | |
return -1; | |
} | |
bool needSend = false; | |
if (_notiActives.ContainsKey(key) && _notiActives[key] == false) | |
{ | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Debug) | |
ShareLogger.Instance.Debug("[AndroidLocalNotification][Send] key {0} This Noti Not Active.", key); | |
needSend = false; | |
} | |
else | |
{ | |
needSend = true; | |
} | |
if (_notis.ContainsKey(key)) | |
{ | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Debug) | |
ShareLogger.Instance.Debug("[AndroidLocalNotification][Send] Try To Send Same Id Noti {0}, So Cancel Old Verstion", key); | |
Cancel(key); | |
} | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Debug) | |
ShareLogger.Instance.Debug("[AndroidLocalNotification][Send] key {0} need = {1}", key, needSend); | |
_notis[key] = @params; | |
if (needSend) | |
{ | |
NotificationManager.SendCustom(new NotificationParams | |
{ | |
Id = key, | |
Delay = @params.FireDate - DateTime.Now, | |
Title = @params.Title, | |
Message = @params.Context, | |
Ticker = @params.Context, | |
Sound = true, | |
Vibrate = true, | |
Light = true, | |
SmallIcon = NotificationIcon.Bell, | |
SmallIconColor = _smallIconColor, | |
LargeIcon = "app_icon", | |
Repeat = @params.IsRepeat, | |
RepeatInterval = ToTimeSpan(@params.RepeatInterval) | |
}); | |
} | |
return key; | |
} | |
public void Cancel(int id) | |
{ | |
Cancel(id, true); | |
} | |
public void Cancel(int id, bool andRemove) | |
{ | |
Debug.LogFormat("[AndroidLocalNotification][Cancel] id {0}", id); | |
NotificationManager.Cancel(id); | |
if (_notis.ContainsKey(id)) | |
{ | |
if (andRemove) | |
{ | |
_notis.Remove(id); | |
LocalNotiParamsSaver.Delete(id); | |
} | |
} | |
else | |
{ | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Debug) | |
ShareLogger.Instance.Debug("[AndroidLocalNotification][Cancel] Not Found id {0}", id); | |
} | |
} | |
public void CancelAll() | |
{ | |
foreach (int id in _notis.Keys.ToArray()) | |
{ | |
LocalNotiParamsSaver.Delete(id); | |
} | |
_notis.Clear(); | |
NotificationManager.CancelAll(); | |
} | |
/// <summary> | |
/// Load Scheduled Notis And Continue Expired Notis | |
/// </summary> | |
/// <param name="keyIds"></param> | |
/// <param name="notis"></param> | |
private void LoadNotisWithoutExpried(string keyIds, Dictionary<int, LocalNotiParams> notis) | |
{ | |
if (PlayerPrefs.HasKey(keyIds)) | |
{ | |
string intAryStr = PlayerPrefs.GetString(keyIds); | |
int[] ids = intAryStr.ToIntAry(); | |
foreach (int id in ids) | |
{ | |
LocalNotiParams noti = LocalNotiParamsSaver.Load(id); | |
if (!noti.IsRepeat && noti.FireDate < DateTime.Now) | |
{ | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Debug) | |
ShareLogger.Instance.Debug("[AndroidLocalNotification][LoadNotis] key {0} it Not Repeat And firedate is Expired , So Remove It.{1}", id, noti.FireDate); | |
LocalNotiParamsSaver.Delete(id); | |
continue; | |
} | |
notis[id] = noti; | |
} | |
} | |
else | |
{ | |
notis.Clear(); | |
} | |
} | |
private void LoadDisabledIds() | |
{ | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Debug) | |
ShareLogger.Instance.Debug("[AndroidLocalNotification][LoadDisabledIds]"); | |
if (PlayerPrefs.HasKey(_keyNotisDisabled)) | |
{ | |
string disabledIdsStr = PlayerPrefs.GetString(_keyNotisDisabled); | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Debug) | |
ShareLogger.Instance.Debug("[AndroidLocalNotification][LoadDisabledIds] disabledIdsStr {0}", disabledIdsStr); | |
int[] disabledIds = disabledIdsStr.ToIntAry(); | |
foreach (int id in disabledIds) | |
{ | |
_notiActives[id] = false; | |
} | |
} | |
else | |
{ | |
_notiActives.Clear(); | |
} | |
} | |
private void SaveNotis(string keyIds, Dictionary<int, LocalNotiParams> notis, bool immediately = false) | |
{ | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Debug) | |
ShareLogger.Instance.Debug("[AndroidLocalNotification][SaveNotis] noti Count {0} ", notis.Count); | |
string intAryStr = notis.Keys.ToArray().ToStr(); | |
PlayerPrefs.SetString(keyIds, intAryStr); | |
foreach (var p in notis) | |
{ | |
LocalNotiParamsSaver.Save(p.Key.ToString(), p.Value); | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Debug) | |
ShareLogger.Instance.Debug("[AndroidLocalNotification][SaveNotis] key {0} , fireDate {1}", p.Key, p.Value.FireDate.ToShortTimeString()); | |
} | |
if (immediately) | |
LocalNotiParamsSaver.Execute(); | |
} | |
private void SaveDisabledIds() | |
{ | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Debug) | |
ShareLogger.Instance.Debug("[AndroidLocalNotification][SaveDisabledIds]"); | |
List<int> disabledIds = new List<int>(); | |
foreach (var p in _notiActives) | |
{ | |
if (p.Value == false) | |
disabledIds.Add(p.Key); | |
} | |
string disabledIdsStr = disabledIds.ToArray().ToStr(); | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Debug) | |
ShareLogger.Instance.Debug("[AndroidLocalNotification][SaveDisabledIds] disabledIdsStr {0}", disabledIdsStr); | |
PlayerPrefs.SetString(_keyNotisDisabled, disabledIdsStr); | |
} | |
private TimeSpan ToTimeSpan(LocalNotiParams.RepeatInteval repeatInteval) | |
{ | |
if (repeatInteval == LocalNotiParams.RepeatInteval.Day) | |
return _perDay; | |
else if (repeatInteval == LocalNotiParams.RepeatInteval.TwoHour) | |
return _perTwoHour; | |
else if (repeatInteval == LocalNotiParams.RepeatInteval.FourHour) | |
return _perFourHour; | |
else if (repeatInteval == LocalNotiParams.RepeatInteval.EightHour) | |
return _perEightHour; | |
else if (repeatInteval == LocalNotiParams.RepeatInteval.OneMinute) | |
return _perOneMin; | |
return TimeSpan.Zero; | |
} | |
private int RandId() | |
{ | |
return UnityEngine.Random.Range(0, int.MaxValue); | |
} | |
private void PushBack() | |
{ | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Debug) | |
ShareLogger.Instance.Debug("[AndroidLocalNotification][PushBack]"); | |
foreach (int key in _notis.Keys.ToArray()) | |
{ | |
if (_notiActives.ContainsKey(key) && _notiActives[key] == false) | |
continue; | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Debug) | |
ShareLogger.Instance.Debug("[AndroidLocalNotification][PushBack] key {0}", key); | |
LocalNotiParams notiParam = _notis[key]; | |
if (notiParam.IsRepeat == true) | |
{ | |
TimeSpan repeatT = ToTimeSpan(notiParam.RepeatInterval); | |
long nowTicks = DateTime.Now.Ticks; | |
long fireDateTicks = notiParam.FireDate.Ticks; | |
//Try Fix FireDate To Over NowTicks | |
if (nowTicks > fireDateTicks) | |
{ | |
long offT = nowTicks - fireDateTicks; | |
int repeatCount = (int)(offT / repeatT.Ticks) + 1; | |
//計算下一次更新時間 | |
TimeSpan nextAddReatTime = new TimeSpan(repeatT.Ticks * repeatCount); | |
notiParam.FireDate = notiParam.FireDate + nextAddReatTime; | |
_notis[key] = notiParam; | |
} | |
} | |
Send(_notis[key], key); | |
} | |
} | |
public void CancelDisplayed() | |
{ | |
NotificationManager.CancelAll(); | |
PushBack(); | |
} | |
} | |
#endif | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using JFun.Foundation.Loggers; | |
using JFun.Framework; | |
using UnityEngine; | |
namespace JFun.Gameplay.CreateWorld | |
{ | |
public enum LocalNotiKey | |
{ | |
Test1 = -1, | |
Test2 = -2, | |
None = 0, | |
DailyReward1 = 1, | |
DailyReward2 = 2, | |
DailyReward3 = 3, | |
GridReward = 4, | |
QuestOnMax = 6, | |
OfflineTooLong = 8, | |
} | |
public interface ILocalNotification | |
{ | |
/// <summary> | |
/// Is Pending Notice And Not Out Of Time. | |
/// </summary> | |
/// <param name="key"></param> | |
/// <returns></returns> | |
bool IsPendingNoti(LocalNotiKey key); | |
void SetActive(LocalNotiKey key, bool isActive); | |
/// <summary> | |
/// unique key , If Scheduled Same Key , Overwrite old vertion. | |
/// </summary> | |
/// <param name="key"></param> | |
/// <param name="params"></param> | |
void Send(LocalNotiKey key, LocalNotiParams @params); | |
void Cancel(LocalNotiKey key); | |
/// <summary> | |
/// Is Pending Notice And Not Out of Time. | |
/// </summary> | |
/// <param name="id"></param> | |
/// <returns></returns> | |
bool IsPendingNoti(int id); | |
void SetActive(int id, bool isEnable); | |
/// <summary> | |
/// Send A Schedule , Return RandKey | |
/// </summary> | |
/// <param name="params"></param> | |
/// <returns></returns> | |
int Send(LocalNotiParams @params); | |
void Cancel(int id); | |
void CancelAll(); | |
void CancelDisplayed(); | |
} | |
[Serializable] | |
public struct LocalNotiParams | |
{ | |
public enum RepeatInteval | |
{ | |
/// <summary> | |
/// Usually for test | |
/// </summary> | |
OneMinute = 1, | |
TwoHour = 2, | |
FourHour = 4, | |
EightHour = 8, | |
Day = 24, | |
} | |
public DateTime FireDate; | |
public string Title; | |
public string Context; | |
public int BadgeNum; | |
public bool IsRepeat; | |
public RepeatInteval RepeatInterval; | |
} | |
/// <summary> | |
/// Invoke Execute() or Set immediately , It will be turly Save in Device. | |
/// </summary> | |
public static class LocalNotiParamsSaver | |
{ | |
private const string _keyFireDate = "LocalNoti_FireDate_"; | |
private const string _keyTitle = "LocalNoti_Title_"; | |
private const string _keyContext = "LocalNoti_Context_"; | |
private const string _keyIsRepeat = "LocalNoti_IsRepeat_"; | |
private const string _keyRepeatInteval = "LocalNoti_RepeatInteval_"; | |
/// <summary> | |
/// Turly Save In Device. | |
/// </summary> | |
public static void Execute() | |
{ | |
PlayerPrefs.Save(); | |
} | |
/// <summary> | |
/// Save to Local | |
/// </summary> | |
/// <param name="key"></param> | |
/// <param name="params"></param> | |
/// <param name="immediately"></param> | |
public static void Save(int key, LocalNotiParams @params, bool immediately = false) | |
{ | |
Save(key.ToString(), @params, immediately); | |
} | |
public static void Save(string key, LocalNotiParams @params, bool immediately = false) | |
{ | |
PlayerPrefs.SetString(_keyFireDate + key, @params.FireDate.Ticks.ToString()); | |
PlayerPrefs.SetString(_keyTitle + key, @params.Title); | |
PlayerPrefs.SetString(_keyContext + key, @params.Context); | |
PlayerPrefs.SetInt(_keyRepeatInteval + key, (int)@params.RepeatInterval); | |
PlayerPrefsExtension.SetBool(_keyIsRepeat + key, @params.IsRepeat); | |
if (immediately) | |
PlayerPrefs.Save(); | |
} | |
/// <summary> | |
/// Load Form Local | |
/// </summary> | |
/// <param name="key"></param> | |
/// <returns></returns> | |
public static LocalNotiParams Load(int key) | |
{ | |
return Load(key.ToString()); | |
} | |
public static LocalNotiParams Load(string key) | |
{ | |
LocalNotiParams load = new LocalNotiParams(); | |
string timeTicksStr = PlayerPrefs.GetString(_keyFireDate + key); | |
string title = PlayerPrefs.GetString(_keyTitle); | |
string context = PlayerPrefs.GetString(_keyContext); | |
bool isRepeat = PlayerPrefsExtension.GetBool(_keyIsRepeat + key); | |
LocalNotiParams.RepeatInteval repeatInteval = (LocalNotiParams.RepeatInteval)PlayerPrefs.GetInt(_keyRepeatInteval + key); | |
long timeTisksLong; | |
if (long.TryParse(timeTicksStr, out timeTisksLong)) | |
{ | |
load.FireDate = new DateTime(timeTisksLong); | |
} | |
else | |
{ | |
ShareLogger.Instance.Error("[LocalNotiParams][Load] key {0} timeTicksStr {1}", key, timeTicksStr); | |
load.FireDate = DateTime.MinValue; | |
} | |
load.Title = title; | |
load.Context = context; | |
load.IsRepeat = isRepeat; | |
load.RepeatInterval = repeatInteval; | |
ShareLogger.Instance.Debug("[LocalNotiParams][Load] key {0} , isRepeat {1}, fireDate {2}", key, load.IsRepeat, load.FireDate); | |
return load; | |
} | |
/// <summary> | |
/// Delete Local Data | |
/// </summary> | |
/// <param name="key"></param> | |
public static void Delete(int key, bool immediately = false) | |
{ | |
Delete(key.ToString(), immediately); | |
} | |
public static void Delete(string key, bool immediately = false) | |
{ | |
PlayerPrefs.DeleteKey(_keyFireDate + key); | |
PlayerPrefs.DeleteKey(_keyTitle); | |
PlayerPrefs.DeleteKey(_keyContext); | |
PlayerPrefs.DeleteKey(_keyIsRepeat + key); | |
PlayerPrefs.DeleteKey(_keyRepeatInteval + key); | |
if (immediately) | |
PlayerPrefs.Save(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using JFun.Framework; | |
using System.Linq; | |
using UnityEngine; | |
using JFun.Foundation.Loggers; | |
namespace JFun.Gameplay.CreateWorld | |
{ | |
#if UNITY_IOS || UNITY_EDITOR | |
public class IOSLocalNotification : ILocalNotification | |
{ | |
private const string _keyNotis = "IOS_LocalNotis_Ids"; | |
private const string _keyNotisDisabled = "IOS_LocalNotis_Ids_Disabled"; | |
private Dictionary<int, LocalNotiParams> _notis = new Dictionary<int, LocalNotiParams>(); | |
private Dictionary<int, bool> _notiActives = new Dictionary<int, bool>(); | |
public IOSLocalNotification(IAppPause iAppPause) | |
{ | |
UnityEngine.iOS.NotificationServices.RegisterForNotifications( | |
UnityEngine.iOS.NotificationType.Alert | | |
UnityEngine.iOS.NotificationType.Badge | | |
UnityEngine.iOS.NotificationType.Sound); | |
iAppPause.OnAppPauseEvent += OnAppPauseEvent; | |
InitNotis(); | |
} | |
private void OnAppPauseEvent(bool pause) | |
{ | |
//OnBackGround | |
if (pause) | |
{ | |
SaveAllNotis(); | |
} | |
//On ForeGround | |
else | |
{ | |
InitNotis(); | |
} | |
} | |
public void PrintNotis() | |
{ | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Debug) | |
{ | |
ShareLogger.Instance.Debug("[IOSLocalNotification][PrintNotis] notis Count {0}", _notis.Count); | |
foreach (var p in _notis) | |
{ | |
ShareLogger.Instance.Debug("[IOSLocalNotification][PrintNotis] _notis id {0} , fireDate {1} , title {2} Context {3}" | |
, p.Key, p.Value.FireDate, p.Value.Title, p.Value.Context); | |
} | |
foreach (var p in _notiActives) | |
{ | |
ShareLogger.Instance.Debug("[IOSLocalNotification][PrintNotis] _notiActives key {0} , Value {1}", p.Key, p.Value); | |
} | |
} | |
} | |
public void SetActive(LocalNotiKey key, bool isActive) | |
{ | |
SetActive((int)key, isActive); | |
} | |
public void SetActive(int id, bool isActive) | |
{ | |
_notiActives[id] = isActive; | |
if (_notis.ContainsKey(id)) | |
{ | |
if (isActive) | |
{ | |
Send(_notis[id], id); | |
} | |
else | |
{ | |
Cancel(id, false); | |
} | |
} | |
} | |
public void Send(LocalNotiKey key, LocalNotiParams @params) | |
{ | |
Send(@params, (int)key); | |
} | |
public void Cancel(LocalNotiKey key) | |
{ | |
Cancel((int)key); | |
} | |
public bool IsPendingNoti(LocalNotiKey key) | |
{ | |
return IsPendingNoti((int)key); | |
} | |
public bool IsPendingNoti(int id) | |
{ | |
if (!_notis.ContainsKey(id)) | |
return false; | |
return _notis[id].FireDate > DateTime.Now; | |
} | |
public int Send(LocalNotiParams @params) | |
{ | |
return Send(@params, RandId()); | |
} | |
public int Send(LocalNotiParams @params, int id) | |
{ | |
if (@params.FireDate < DateTime.Now) | |
{ | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Warn) | |
ShareLogger.Instance.Warn("[IOSLocalNotification][Send] FireDate < DateTime Now . You Can't See This Notifaction"); | |
return -1; | |
} | |
bool needSend = false; | |
if (_notiActives.ContainsKey(id) && _notiActives[id] == false) | |
{ | |
needSend = false; | |
} | |
else | |
{ | |
needSend = true; | |
} | |
if (_notis.ContainsKey(id)) | |
{ | |
Cancel(id); | |
} | |
_notis[id] = @params; | |
if (needSend) | |
{ | |
if (@params.IsRepeat == false) | |
{ | |
UnityEngine.iOS.LocalNotification notif = Parser(@params); | |
SingleSend(notif, id, 0); | |
} | |
else | |
{ | |
if (@params.RepeatInterval == LocalNotiParams.RepeatInteval.OneMinute) | |
{ | |
UnityEngine.iOS.LocalNotification notif = Parser(@params); | |
notif.repeatInterval = UnityEngine.iOS.CalendarUnit.Minute; | |
SingleSend(notif, id, 0); | |
} | |
else | |
{ | |
int times = 1; | |
if (@params.RepeatInterval == LocalNotiParams.RepeatInteval.Day) | |
times = 24 / 24; | |
else if (@params.RepeatInterval == LocalNotiParams.RepeatInteval.EightHour) | |
times = 24 / 8; | |
else if (@params.RepeatInterval == LocalNotiParams.RepeatInteval.FourHour) | |
times = 24 / 4; | |
else if (@params.RepeatInterval == LocalNotiParams.RepeatInteval.TwoHour) | |
times = 24 / 2; | |
UnityEngine.iOS.LocalNotification notif = Parser(@params); | |
DateTime fireDateOG = @params.FireDate; | |
for (int i = 0; i < times; i++) | |
{ | |
notif.fireDate = fireDateOG + TimeSpan.FromHours(8 * i); | |
notif.repeatInterval = UnityEngine.iOS.CalendarUnit.Day; | |
SingleSend(notif, id, i); | |
} | |
} | |
} | |
} | |
return id; | |
} | |
/// <summary> | |
/// Fully Clear All Notification , Not RePush It. | |
/// </summary> | |
public void CancelAll() | |
{ | |
foreach (int id in _notis.Keys.ToArray()) | |
{ | |
LocalNotiParamsSaver.Delete(id); | |
} | |
_notis.Clear(); | |
CleanAll(); | |
} | |
public void Cancel(int id) | |
{ | |
Cancel(id, true); | |
} | |
public void Cancel(int id, bool andRemove) | |
{ | |
if (_notis.ContainsKey(id)) | |
{ | |
if (andRemove) | |
{ | |
CleanAll(); | |
//Remove by Id | |
_notis.Remove(id); | |
LocalNotiParamsSaver.Delete(id); | |
//PushBack | |
PushBack(); | |
} | |
else | |
{ | |
CleanAll(); | |
LocalNotiParams n = _notis[id]; | |
_notis.Remove(id); | |
PushBack(); | |
_notis[id] = n; | |
} | |
} | |
else | |
{ | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Debug) | |
ShareLogger.Instance.Debug("[IOSLocalNotification][Cancel] Not Found Noti id = [0]", id); | |
} | |
} | |
/// <summary> | |
/// Clean All Notfication And RePush That | |
/// </summary> | |
private void InitNotis() | |
{ | |
_notis.Clear(); | |
LoadDisabledIds(); | |
LoadNotisWithoutExpried(_keyNotis, _notis); | |
//Clean All Noti And Badge , And RePush Back. | |
CleanAll(); | |
PushBack(); | |
} | |
private void SaveAllNotis() | |
{ | |
SaveNotis(_keyNotis, _notis); | |
SaveDisabledIds(); | |
LocalNotiParamsSaver.Execute(); | |
} | |
private void PushBack() | |
{ | |
foreach (int key in _notis.Keys.ToArray()) | |
{ | |
if (_notiActives.ContainsKey(key) && _notiActives[key] == false) | |
continue; | |
Send(_notis[key], key); | |
} | |
} | |
private void SaveDisabledIds() | |
{ | |
List<int> disabledIds = new List<int>(); | |
foreach (var p in _notiActives) | |
{ | |
if (p.Value == false) | |
disabledIds.Add(p.Key); | |
} | |
string disabledIdsStr = disabledIds.ToArray().ToStr(); | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Debug) | |
ShareLogger.Instance.Debug("[IOSLocalNotification][SaveDisabledIds] disabledIdsStr {0}", disabledIdsStr); | |
PlayerPrefs.SetString(_keyNotisDisabled, disabledIdsStr); | |
} | |
private void SaveNotis(string keyIds, Dictionary<int, LocalNotiParams> notis) | |
{ | |
string intAryStr = notis.Keys.ToArray().ToStr(); | |
PlayerPrefs.SetString(keyIds, intAryStr); | |
foreach (var p in notis) | |
{ | |
LocalNotiParamsSaver.Save(p.Key, p.Value); | |
} | |
} | |
private void LoadDisabledIds() | |
{ | |
if (PlayerPrefs.HasKey(_keyNotisDisabled)) | |
{ | |
string disabledIdsStr = PlayerPrefs.GetString(_keyNotisDisabled); | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Debug) | |
ShareLogger.Instance.Debug("[IOSLocalNotification][LoadDisabledIds] disabledIdsStr {0}", disabledIdsStr); | |
int[] disabledIds = disabledIdsStr.ToIntAry(); | |
foreach (int id in disabledIds) | |
{ | |
_notiActives[id] = false; | |
} | |
} | |
else | |
{ | |
_notiActives.Clear(); | |
} | |
} | |
private void LoadNotisWithoutExpried(string keyIds, Dictionary<int, LocalNotiParams> notis) | |
{ | |
if (PlayerPrefs.HasKey(keyIds)) | |
{ | |
string intAryStr = PlayerPrefs.GetString(keyIds); | |
int[] ids = intAryStr.ToIntAry(); | |
foreach (int id in ids) | |
{ | |
LocalNotiParams noti = LocalNotiParamsSaver.Load(id); | |
if (noti.IsRepeat == false && noti.FireDate < DateTime.Now) | |
{ | |
LocalNotiParamsSaver.Delete(id); | |
continue; | |
} | |
notis[id] = noti; | |
} | |
} | |
else | |
notis.Clear(); | |
} | |
private UnityEngine.iOS.LocalNotification Parser(LocalNotiParams @params) | |
{ | |
var notif = new UnityEngine.iOS.LocalNotification(); | |
notif.fireDate = @params.FireDate; | |
notif.alertAction = @params.Title; | |
notif.alertBody = @params.Context; | |
notif.soundName = UnityEngine.iOS.LocalNotification.defaultSoundName; | |
//notif.applicationIconBadgeNumber += @params.BadgeNum; | |
notif.applicationIconBadgeNumber = 1; | |
notif.hasAction = true; | |
return notif; | |
} | |
private void SingleSend(UnityEngine.iOS.LocalNotification notif, int id, int index) | |
{ | |
UnityEngine.iOS.NotificationServices.ScheduleLocalNotification(notif); | |
} | |
private void CleanAll() | |
{ | |
// If Only Set BadgeNum = 0 ,It is Not Working, | |
// Have To Cancel And Clear All Notification. | |
UnityEngine.iOS.LocalNotification l = new UnityEngine.iOS.LocalNotification(); | |
l.applicationIconBadgeNumber = 0; | |
l.hasAction = false; | |
UnityEngine.iOS.NotificationServices.PresentLocalNotificationNow(l); | |
UnityEngine.iOS.NotificationServices.CancelAllLocalNotifications(); | |
UnityEngine.iOS.NotificationServices.ClearLocalNotifications(); | |
} | |
private int RandId() | |
{ | |
return UnityEngine.Random.Range(0, int.MaxValue); | |
} | |
public void CancelDisplayed() | |
{ | |
//IOS IS AUTO TODO THIS. | |
} | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment