コード例 #1
0
    /// <summary>
    /// 清空所有本地消息
    /// </summary>
    internal void CleanNotification()
    {
#if UNITY_IPHONE || NEW_EGSDK_IOS
        UnityEngine.LocalNotification l = new UnityEngine.LocalNotification();
        l.applicationIconBadgeNumber = -1;
        NotificationServices.PresentLocalNotificationNow(l);
        NotificationServices.CancelAllLocalNotifications();
        NotificationServices.ClearLocalNotifications();
#endif

#if UNITY_ANDROID || NEW_EGSDK_ANDROID
        // AndroidLocalNotification.CancelNotification(1);
        // AndroidLocalNotification.CancelNotification(2);
        AndroidLocalNotification.CancelAllNotifications();
#endif
    }
コード例 #2
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
    }