// *********************************************************************** // Company : // Author : Kimch // Created : // // Last Modified By : Kimch // Last Modified On : // *********************************************************************** using System; using System.Collections; using UnityEngine; #if UNITY_EDITOR #elif UNITY_IOS //using Unity.Notifications.iOS; #elif UNITY_ANDROID using Unity.Notifications.Android; #endif /// /// KNotification /// public class KNotification : MonoBehaviour { #if UNITY_ANDROID || UNITY_IOS #region FIELD private const string CHANNEL_ID = "channel_id_ltb"; private const string CHANNEL_NAME = "Default Channel"; private const string CHANNEL_DESCRIPTION = "Generic notifications"; #pragma warning disable CS0414 // 字段“KNotification._isFirst”已被赋值,但从未使用过它的值 private bool _isFirst; #pragma warning restore CS0414 // 字段“KNotification._isFirst”已被赋值,但从未使用过它的值 private DateTime _firstDate; #endregion #region PROPERTY #endregion #region METHOD private void CancelAllLocal() { #if UNITY_EDITOR #elif UNITY_IOS UnityEngine.iOS.NotificationServices.CancelAllLocalNotifications(); UnityEngine.iOS.NotificationServices.ClearLocalNotifications(); #elif UNITY_ANDROID AndroidNotificationCenter.CancelAllNotifications(); #endif } private void PushLocal(string title, string alertBody, DateTime fireDate) { #if UNITY_EDITOR #elif UNITY_IOS var local = new UnityEngine.iOS.LocalNotification { fireDate = fireDate, alertBody = alertBody, soundName = UnityEngine.iOS.LocalNotification.defaultSoundName }; UnityEngine.iOS.NotificationServices.ScheduleLocalNotification(local); #elif UNITY_ANDROID var notification = new AndroidNotification { Title = title, Text = alertBody, FireTime = fireDate }; notification.LargeIcon = "icon_0"; notification.SmallIcon = "icon_1"; AndroidNotificationCenter.SendNotification(notification, CHANNEL_ID); #endif } private void RegisterLocal() { #if UNITY_EDITOR #elif UNITY_IOS UnityEngine.iOS.NotificationServices.RegisterForNotifications( UnityEngine.iOS.NotificationType.Alert | UnityEngine.iOS.NotificationType.Badge | UnityEngine.iOS.NotificationType.Sound); #elif UNITY_ANDROID var channel = new AndroidNotificationChannel() { Id = CHANNEL_ID, Name = CHANNEL_NAME, Importance = Importance.Default, Description = CHANNEL_DESCRIPTION, }; AndroidNotificationCenter.RegisterNotificationChannel(channel); void receivedNotificationHandler(AndroidNotificationIntentData data) { var msg = "Notification received : " + data.Id + "\n"; msg += "\n Notification received: "; msg += "\n .Title: " + data.Notification.Title; msg += "\n .Body: " + data.Notification.Text; msg += "\n .Channel: " + data.Channel; Debug.Log(msg); } AndroidNotificationCenter.OnNotificationReceived += receivedNotificationHandler; #endif } private void OnEnterGame() { CancelAllLocal(); } private void OnQuitGame() { var today = System.DateTime.Today; var days = (today - _firstDate).TotalDays; if (days < 1) { //var bodys = new string[] { "体力已满,上来拯救江湖吧." }; //PushLocal("少侠李太白", bodys[UnityEngine.Random.Range(0, bodys.Length)], G.PlayerProxy.Instance.energyRecoverDateTime); } else if (days < 7) { var bodys = new string[] { "望少侠速来,江湖危在旦夕,需要你来拯救." }; PushLocal("少侠李太白", bodys[UnityEngine.Random.Range(0, bodys.Length)], today.AddDays(1).AddHours(12)); } else { var bodys = new string[] { "望少侠速来,江湖危在旦夕,需要你来拯救." }; PushLocal("少侠李太白", bodys[UnityEngine.Random.Range(0, bodys.Length)], today.AddDays(1).AddHours(18)); } } #endregion #region UNITY /// Starts this instance. private IEnumerator Start() { var firstInstall = PlayerPrefs.GetString("first_install"); if (string.IsNullOrEmpty(firstInstall)) { _firstDate = DateTime.Today.Date; PlayerPrefs.SetString("first_install", _firstDate.ToShortDateString()); PlayerPrefs.Save(); _isFirst = true; } else { if (DateTime.TryParse(firstInstall, out DateTime tmpFD)) { _firstDate = tmpFD; } else { _firstDate = DateTime.Today.Date; PlayerPrefs.SetString("first_install", _firstDate.ToShortDateString()); PlayerPrefs.Save(); } } RegisterLocal(); yield return null; OnEnterGame(); } private void OnDisable() { OnQuitGame(); } private void OnApplicationPause(bool pause) { if (pause) { OnQuitGame(); } else { OnEnterGame(); } } #endregion #endif }