private void OnGUI() { // Color is supported only in Android >= 5.0 GUI.enabled = sleepUntil < Time.time; if (GUILayout.Button("5 SECONDS", GUILayout.Height(Screen.height * 0.2f))) { //AndroidLocalNotification.SendNotification(1, 5, "Title", "Long message text", new Color32(0xff, 0x44, 0x44, 255)); DateTime _dt = DateTime.Now; _dt = _dt.AddSeconds(5); ZXNotification.GetInstance().NotificationMessage(3, "测试标题", "测试中文", _dt, false); sleepUntil = Time.time + 5; } if (GUILayout.Button("5 SECONDS BIG ICON", GUILayout.Height(Screen.height * 0.2f))) { AndroidLocalNotification.SendNotification(2, 5, "Title", "Long message text with big icon", new Color32(0xff, 0x44, 0x44, 255), true, true, true, "app_icon"); sleepUntil = Time.time + 5; } if (GUILayout.Button("EVERY 5 SECONDS", GUILayout.Height(Screen.height * 0.2f))) { AndroidLocalNotification.SendRepeatingNotification(1, 5, 5, "Title", "Long message text", new Color32(0xff, 0x44, 0x44, 255)); sleepUntil = Time.time + 99999; } if (GUILayout.Button("10 SECONDS EXACT", GUILayout.Height(Screen.height * 0.2f))) { AndroidLocalNotification.SendNotification(3, 10, "Title", "Long exact message text", new Color32(0xff, 0x44, 0x44, 255), executeMode: AndroidLocalNotification.NotificationExecuteMode.ExactAndAllowWhileIdle); sleepUntil = Time.time + 10; } GUI.enabled = true; if (GUILayout.Button("STOP", GUILayout.Height(Screen.height * 0.2f))) { AndroidLocalNotification.CancelNotification(1); AndroidLocalNotification.CancelAllNotifications(); sleepUntil = 0; } }
/// <summary> /// 本地推送 你可以传入一个固定的推送时间 /// </summary> /// <param name="_message"></param> /// <param name="_date"></param> /// <param name="_isRepeatDay"></param> internal void NotificationMessage(int _id, string _title, string _message, DateTime _date, bool _isRepeatDay) { // 推送时间需要大于当前时间 if (_date <= System.DateTime.Now) { // 往前翻一天 _date = _date.AddDays(1); } #if UNITY_IPHONE || NEW_EGSDK_IOS // 推送时间需要大于当前时间 // if (_date > System.DateTime.Now) { UnityEngine.LocalNotification localNotification = new UnityEngine.LocalNotification(); localNotification.fireDate = _date; localNotification.alertBody = _message; localNotification.applicationIconBadgeNumber = 1; localNotification.hasAction = true; localNotification.alertAction = _title; if (_isRepeatDay) { // 是否每天定期循环 localNotification.repeatCalendar = CalendarIdentifier.ChineseCalendar; localNotification.repeatInterval = CalendarUnit.Day; } localNotification.soundName = UnityEngine.LocalNotification.defaultSoundName; NotificationServices.ScheduleLocalNotification(localNotification); } #endif #if UNITY_ANDROID || NEW_EGSDK_ANDROID // 前面已经前翻一天 // if (_date > System.DateTime.Now) { long _delay; //if (_date.Hour >= 12) //{ // _delay = __SECONDOFDAY - ((_date.Hour - 12) * __SECONDOFHOUR + _date.Minute * __SECONDOFMINUTE + _date.Second); //} //else //{ // _delay = (12 - _date.Hour) * __SECONDOFHOUR - _date.Minute * __SECONDOFMINUTE - _date.Second; //} // 计算时间差 目标时间-当前时间获取时间差 TimeSpan _ts = (TimeSpan)(_date - System.DateTime.Now); _delay = (long)_ts.TotalSeconds; DebugLog.Log("dateTime:" + _date.ToString()); DebugLog.Log("delay:" + _delay); if (_isRepeatDay) { AndroidLocalNotification.SendRepeatingNotification(_id, _delay, __SECONDOFDAY, _title, _message, new Color32(0xff, 0x44, 0x44, 255), true, true, true); } else { AndroidLocalNotification.SendNotification(_id, _delay, _title, _message, new Color32(0xff, 0x44, 0x44, 255), true, true, true); } } #endif }