/// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        protected virtual bool ShowLater(NotificationRequest request)
        {
            if (request.Schedule.NotifyTime is null ||
                request.Schedule.NotifyTime <= DateTime.Now) // To be consistent with iOS, Do not Schedule notification if NotifyTime is earlier than DateTime.Now
            {
                return(false);
            }

            var dictionaryRequest = NotificationCenter.GetRequestSerialize(request);

            var intent = new Intent(Application.Context, typeof(ScheduledAlarmReceiver));

            foreach (var item in dictionaryRequest)
            {
                intent.PutExtra(item.Key, item.Value);
            }
            var pendingIntent = PendingIntent.GetBroadcast(
                Application.Context,
                request.NotificationId,
                intent,
                PendingIntentFlags.UpdateCurrent
                );

            var utcAlarmTimeInMillis = (request.Schedule.NotifyTime.Value.ToUniversalTime() - DateTime.UtcNow).TotalMilliseconds;
            var triggerTime          = (long)utcAlarmTimeInMillis;

            if (request.Schedule.RepeatType != NotificationRepeat.No)
            {
                TimeSpan?repeatInterval = null;
                switch (request.Schedule.RepeatType)
                {
                case NotificationRepeat.Daily:
                    // To be consistent with iOS, Schedule notification next day same time.
                    repeatInterval = TimeSpan.FromDays(1);
                    break;

                case NotificationRepeat.Weekly:
                    // To be consistent with iOS, Schedule notification next week same day same time.
                    repeatInterval = TimeSpan.FromDays(7);
                    break;

                case NotificationRepeat.TimeInterval:
                    if (request.Schedule.NotifyRepeatInterval.HasValue)
                    {
                        repeatInterval = request.Schedule.NotifyRepeatInterval.Value;
                    }
                    break;
                }

                if (repeatInterval == null)
                {
                    return(true);
                }
                var intervalTime = (long)repeatInterval.Value.TotalMilliseconds;

                MyAlarmManager.SetInexactRepeating(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + triggerTime, intervalTime, pendingIntent);
            }
            else
            {
                if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
                {
                    MyAlarmManager.SetExactAndAllowWhileIdle(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + triggerTime, pendingIntent);
                }
                else
                {
                    MyAlarmManager.SetExact(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + triggerTime, pendingIntent);
                }
            }

            AddPreferencesNotificationId(PreferencesPendingIdListKey, request.NotificationId);

            return(true);
        }
Beispiel #2
0
        /// <inheritdoc />
        public async Task <bool> Show(NotificationRequest request)
        {
            UNNotificationTrigger trigger = null;

            try
            {
                if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0) == false)
                {
                    return(false);
                }

                if (request is null)
                {
                    return(false);
                }

                var allowed = await NotificationCenter.AskPermissionAsync().ConfigureAwait(false);

                if (allowed == false)
                {
                    return(false);
                }

                var userInfoDictionary = new NSMutableDictionary();
                var dictionary         = NotificationCenter.GetRequestSerialize(request);
                foreach (var item in dictionary)
                {
                    userInfoDictionary.SetValueForKey(new NSString(item.Value), new NSString(item.Key));
                }

                using (var content = new UNMutableNotificationContent
                {
                    Title = request.Title,
                    Subtitle = request.Subtitle,
                    Body = request.Description,
                    Badge = request.BadgeNumber,
                    UserInfo = userInfoDictionary,
                    Sound = UNNotificationSound.Default,
                })
                {
                    // Image Attachment
                    if (request.Image != null)
                    {
                        var nativeImage = await GetNativeImage(request.Image);

                        if (nativeImage != null)
                        {
                            content.Attachments = new[] { nativeImage };
                        }
                    }

                    if (request.CategoryType != NotificationCategoryType.None)
                    {
                        content.CategoryIdentifier = ToNativeCategory(request.CategoryType);
                    }

                    if (string.IsNullOrWhiteSpace(request.Sound) == false)
                    {
                        content.Sound = UNNotificationSound.GetSound(request.Sound);
                    }

                    var repeats = request.Schedule.RepeatType != NotificationRepeat.No;

                    if (repeats && request.Schedule.RepeatType == NotificationRepeat.TimeInterval &&
                        request.Schedule.NotifyRepeatInterval.HasValue)
                    {
                        TimeSpan interval = request.Schedule.NotifyRepeatInterval.Value;

                        // Cannot delay and repeat in when TimeInterval
                        trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(interval.TotalSeconds, true);
                    }
                    else
                    {
                        using (var notifyTime = GetNsDateComponentsFromDateTime(request))
                        {
                            trigger = UNCalendarNotificationTrigger.CreateTrigger(notifyTime, repeats);
                        }
                    }

                    var notificationId =
                        request.NotificationId.ToString(CultureInfo.CurrentCulture);

                    var nativeRequest = UNNotificationRequest.FromIdentifier(notificationId, content, trigger);

                    await UNUserNotificationCenter.Current.AddNotificationRequestAsync(nativeRequest)
                    .ConfigureAwait(false);

                    return(true);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return(false);
            }
            finally
            {
                trigger?.Dispose();
            }
        }