Exemplo n.º 1
0
        public void Publish(NotificationInfo notificationInfo, DateTime?notifyTime = null)
        {
            // EARLY OUT: app doesn't have permissions
            if (!_hasNotificationsPermission)
            {
                return;
            }

            _messageId++;

            var content = new UNMutableNotificationContent();

            content.SetNotificationInfo(notificationInfo);

            UNNotificationTrigger trigger;

            if (notifyTime != null)
            {
                // Create a calendar-based trigger.
                trigger = UNCalendarNotificationTrigger.CreateTrigger(GetNSDateComponents(notifyTime.Value), false);
            }
            else
            {
                // Create a time-based trigger, interval is in seconds and must be greater than 0.
                trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(0.25, false);
            }

            var request = UNNotificationRequest.FromIdentifier(_messageId.ToString(), content, trigger);

            UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) =>
            {
                if (err != null)
                {
                    throw new Exception($"Failed to schedule notification: {err}");
                }
            });
        }