chicken_dy/Assets/Scripts/ToukaGames/AnalyticsScripts/OtherScripts/LocalNotification/LocalNotificationManager.cs

174 lines
6.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Touka.GameLogic;
using System;
/// <summary>
/// 本地通知类
/// 对应配置在 StaticOtherConfig.cs 文件中修改
///
/// 1. 打开 StaticOtherConfig里的开关 LocalNotitcifaction_Switch
/// 2. 修改对应中英文推送内容即可
/// </summary>
public class LocalNotificationManager : ToukaSingletonMonoBehaviour<LocalNotificationManager>
{
//private string morrowMsgCN = "敌人越来越厉害了,我需要你的帮助!|||今日奖励已经准备好了,上线领取吧!|||家园在你的管理下能扩大多少版图呢?";
//private string day357MsgCN = "确认过眼神,是今天还没有上线的人|||我们的口号就是开疆扩土搞开发!|||最新玩法已升级,更多挑战等着你!";
//private string day468MsgCN = "快来打造属于你的王国!|||这么精美的场景设定,是梦想中的国度没错了!|||金币收益已经爆满,上线领取一波?";
//private string morrowMsgEN = "I keep getting beat by enemies. COME HELP!!|||Come and expand your territory|||I can't win this game without your help";
//private string day357MsgEN = "For our homeland, you must defeat the enemy|||New challenge has been unlocked.|||You are a true warrior who defeated the enemy";
//private string day468MsgEN = "Help me repel the enemy, only you can do it|||There are too many enemies, what should I do?|||New reward is ready, go online to claim it!";
private void Awake()
{
RegisterLocalNotification();
}
public void Init()
{
}
private void OnApplicationFocus(bool isFocus)
{
//Debug.Log("isFocus : " + isFocus);
ClearAllLocalNotifications();
if (!isFocus)
{
LocalAddCommonLocalNotifications();
}
}
#region
//通知的发送规则设计为:
//1.第2日的通知如果玩家在6-22点之间登陆游戏在第2日的相同登陆时间触发通知。如果玩家在22-6点之间登陆游戏在第2日的22点触发通知。
//2.第3、5、7日的通知中午12点触发通知
//3.第4、6、8日的通知晚上6点触发通知
//
//通知内容配置在MessageTemplate中。
//第2日的通知使用的Key为呢notifi:nextday,
//第3、5、7日的通知使用的Key为notifi:thirday,
//第4、6、8日的通知使用的Key为notifi:fourthday
//如果需要配置多条不同的消息随机,那么请使用"|||"来分隔不同的消息
public void LocalAddCommonLocalNotifications()
{
DateTime now;
DateTime delay;
now = DateTime.Now;
//当前是早上6点到晚上10点
if (now.Hour >= 6 && now.Hour <= 22)
{
//次日同点触发
AddLocalNotification(getRandomString(0), now.AddDays(1));
}
else
{
//次日晚上10点触发
int hourMorrow = StaticOtherConfig.MorrowHourNum;
int minMorrow = StaticOtherConfig.MorrowMinuteNum;
delay = new DateTime(now.Year, now.Month, now.Day, hourMorrow, minMorrow, 0).AddDays(1);
AddLocalNotification(getRandomString(0), delay);
}
//第3 ,5, 7天 中午12点触发通知
for (int i = 2; i <= 6; i += 2)
{
int hourMorrow = StaticOtherConfig.Day357HourNum;
int minMorrow = StaticOtherConfig.Day357MinuteNum;
delay = new DateTime(now.Year, now.Month, now.Day, hourMorrow, minMorrow, 0).AddDays(i);
AddLocalNotification(getRandomString(1), delay);
}
//第 46,8 天 下午18点触发通知
for (int i = 3; i <= 7; i += 2)
{
int hourMorrow = StaticOtherConfig.Day468HourNum;
int minMorrow = StaticOtherConfig.Day468MinuteNum;
delay = new DateTime(now.Year, now.Month, now.Day, hourMorrow, minMorrow, 0).AddDays(i);
AddLocalNotification(getRandomString(2), delay);
}
}
/// <summary>
/// 从第 N 组推送用于中随机一句
/// </summary>
/// <param name="_indexFlg"> 第几组 </param>
/// <returns></returns>
string getRandomString(int _indexFlg)
{
string pushInfo = "";
switch (_indexFlg)
{
case 0:
pushInfo = isChinese() ? StaticOtherConfig.MorrowMsgCN : StaticOtherConfig.MorrowMsgEN;
break;
case 1:
pushInfo = isChinese() ? StaticOtherConfig.Day357MsgCN : StaticOtherConfig.Day357MsgEN;
break;
case 2:
pushInfo = isChinese() ? StaticOtherConfig.Day468MsgCN : StaticOtherConfig.Day468MsgEN;
break;
}
string[] s = pushInfo.Split(new string[] { "|||" }, StringSplitOptions.None);
return s[UnityEngine.Random.Range(0, s.Length)];
}
/// <summary>
/// 是否是中文系统
/// 中文系统显示中文推送
/// </summary>
/// <returns></returns>
private bool isChinese()
{
bool tIsCurrentChinese = Application.systemLanguage == SystemLanguage.Chinese || Application.systemLanguage == SystemLanguage.ChineseSimplified || Application.systemLanguage == SystemLanguage.ChineseTraditional;
return tIsCurrentChinese;
}
/// <summary>
/// 注册本地通知
/// </summary>
private void RegisterLocalNotification()
{
Debug.Log("register apns first");
#if UNITY_IOS && !NO_SDK
TGTools.Instance.RegisterAPNS();
#endif
}
/// <summary>
/// 清除全部已注册的本地通知
/// </summary>
private void ClearAllLocalNotifications()
{
Debug.Log("Clear all local notifications");
#if UNITY_IOS && !NO_SDK
TGTools.Instance.RemoveAllNotification();
#endif
}
/// <summary>
/// 添加一条本地通知
/// </summary>
/// <param name="_pushInfo"></param>
/// <param name="_time"></param>
private void AddLocalNotification(string _pushInfo, DateTime _time)
{
string timeStr = _time.ToString("yyyy-MM-dd HH:mm:ss");
//Debug.Log("AddLocalNotification, time : " + timeStr + " , info : " + _pushInfo);
#if UNITY_IOS && !NO_SDK
TGTools.Instance.RegisterNotification(timeStr, _pushInfo, timeStr);
#endif
}
#endregion
}