Exemplo n.º 1
0
    void RemoveAllNotifications()
    {
        #region Android
#if UNITY_ANDROID
        string raw = PlayerPrefs.GetString("NotificationIDs");
        if (!string.IsNullOrEmpty(raw))
        {
            notification_ids = raw.Split(',').Select(x => x.Split(':')).ToDictionary(x => x[0], x => int.Parse(x[1]));
        }
        else
        {
            return;
        }
        foreach (var key in notification_ids.Values)
        {
            AndroidLocalNotification.CancelNotification(key);
        }
        notification_ids.Clear();
        PlayerPrefs.DeleteKey("NotificationIDs");
#endif
        #endregion

        #region iOS
#if UNITY_IOS
        NotificationServices.CancelAllLocalNotifications();
#endif
        #endregion
    }
        internal static Task <bool> Show(AndroidLocalNotification notification)
        {
            var native = notification.Render(UIRuntime.CurrentActivity, CurrentChannel?.Id);

            NotificationManager.Notify(notification.Id, native);

            EnsureScreenLightIsOn();

            return(Task.FromResult(result: true));
        }
        static PendingIntent CreateAlarmHandlerIntent(int id, AndroidLocalNotification notification = null)
        {
            var result = new Intent(UIRuntime.CurrentActivity, typeof(ScheduledNotificationsBroadcastReceiver))
                         .SetAction($"ScheduledNotification-{id}");

            if (notification is not null)
            {
                result.PutExtra(LocalNotificationKey, JsonConvert.SerializeObject(notification));
            }

            return(result.ToPendingBroadcast());
        }
Exemplo n.º 4
0
 private void scheduleLocalNotification(string id, string title, string description, long offsetTicks, int badgeNumber)
 {
     if (App.Binder.NotificationRegister.Registered)
     {
         AndroidLocalNotification notification = new AndroidLocalNotification();
         notification.fireDate = DateTime.Now + new TimeSpan(offsetTicks);
         notification.applicationIconBadgeNumber = badgeNumber;
         notification.hasAction   = true;
         notification.alertBody   = description;
         notification.alertAction = title;
         AndroidNotificationServices.ScheduleLocalNotification(notification);
     }
 }
    /// <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
    }
Exemplo n.º 6
0
    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
    }