Exemplo n.º 1
0
 public IDisposable RegisterRequest(ToastId toastId, Action onShown, Action onTapped)
 {
     lock (requests)
         requests[toastId] = new Request(onShown, onTapped);
     UNUserNotificationCenter.Current.Delegate = this;
     return(new RegistrationToken(this, toastId));
 }
 void SetContentIntent(IPlatformNotificationBuilder builder, ToastId toastId)
 {
     builder.SetContentIntent(
         builder.GetForceOpenAppOnNotificationTap()
         ? CreateLaunchIntent(builder, toastId)
         : CreateContentOrDeleteIntent(IntentConstants.KTapped, builder, toastId));
 }
Exemplo n.º 3
0
        public Task <NotificationResult> ShowAsync(out ToastId toastId, CancellationToken cancellationToken)
        {
            var tcs = new TaskCompletionSource <NotificationResult>();
            var tn  = new ToastNotification(xmlDocument);

            tn.Tag = string.IsNullOrEmpty(notificationBuilder.Tag) ? Guid.NewGuid().ToString() : notificationBuilder.Tag;
            if (string.IsNullOrEmpty(notificationBuilder.Group) == false)
            {
                tn.Group = notificationBuilder.Group;
            }
            if (string.IsNullOrEmpty(notificationBuilder.RemoteId) == false)
            {
                tn.RemoteId = notificationBuilder.RemoteId;
            }
            tn.Activated += (o, e) => tcs.TrySetResult(NotificationResult.Activated);
            tn.Dismissed += (o, e) => tcs.TrySetResult(ToNotificationResult(e.Reason));
            tn.Failed    += (o, e) => tcs.TrySetException(new NotificationException(e.ErrorCode.Message, e.ErrorCode));
            ToastNotificationManager.CreateToastNotifier().Show(tn);
            toastId = ToastId.FromNotification(tn);

            if (cancellationToken.CanBeCanceled)
            {
                return(tcs.WatchCancellationAsync(cancellationToken, () => ToastNotificationManager.History.Remove(tn.Tag)));
            }
            return(tcs.Task);
        }
            public override void OnReceive(Context?context, Intent?intent)
            {
                if (intent == null)
                {
                    return;
                }
                var toastId = ToastId.FromIntent(intent);

                if (toastId == null)
                {
                    return;
                }
                var tcs = hrIntentManager.Value.PopTask(toastId);

                switch (intent.Action)
                {
                case IntentConstants.KTapped:
                    OnTapped(toastId, tcs);
                    break;

                case IntentConstants.KScheduled:
                    OnScheduled(toastId, intent);
                    break;

                default:
                    tcs?.TrySetResult(NotificationResult.UserCanceled);
                    break;
                }
            }
Exemplo n.º 5
0
        public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
        {
            var tid = new ToastId(response.Notification.Request.Identifier);

            GetRequest(tid)?.OnTapped();
            systemEventSource.SendEvent(new NotificationEvent(tid));
            completionHandler();
        }
Exemplo n.º 6
0
        public ScheduledToastCancellation(ScheduledToastNotification toastNotification)
        {
            this.toastNotifier     = ToastNotificationManager.CreateToastNotifier();
            this.toastNotification = toastNotification;
            ToastId = ToastId.FromNotification(toastNotification);

            toastNotifier.AddToSchedule(toastNotification);
        }
Exemplo n.º 7
0
        public void GetPersistentHashCode(int expected, int id, string tag)
        {
            // preapre
            var tid = new ToastId(id, tag);

            // act & verify
            Assert.Equal(expected, tid.GetPersistentHashCode());
        }
            private void OnScheduled(ToastId toastId, Intent intent)
            {
                var notification = (global::Android.App.Notification?)intent.GetParcelableExtra(IntentConstants.KNotification);

                if (notification != null)
                {
                    hrIntentManager.Value.androidNotificationManager.Notify(notification, toastId);
                }
            }
Exemplo n.º 9
0
        public void Equals_()
        {
            var someId = Guid.NewGuid().ToString();
            var left   = new ToastId(someId);
            var right  = new ToastId(someId);

            Assert.Equal(left, right);
            Assert.Equal(left.GetHashCode(), right.GetHashCode());
        }
Exemplo n.º 10
0
        public void RemoveScheduled(ToastId toastId)
        {
            var notification = notifier.GetScheduledToastNotifications().Where(n => n.Tag == toastId.Tag && n.Group == toastId.Group).FirstOrDefault();

            if (notification != null)
            {
                notifier.RemoveFromSchedule(notification);
            }
        }
Exemplo n.º 11
0
        public void Equals_(string tag, string group)
        {
            // prepare
            var left  = new ToastId(tag, group);
            var right = new ToastId(tag, group);

            // act & verify
            Assert.Equal(left, right);
            Assert.Equal(left.GetHashCode(), right.GetHashCode());
        }
Exemplo n.º 12
0
        public void RemoveScheduled(ToastId toastId)
        {
            var pi = intentManager.GetPendingIntentById(toastId);

            if (pi != null)
            {
                AlarmManager.Cancel(pi);
                pi.Cancel();
            }
        }
Exemplo n.º 13
0
        public void Equals_(int id, string tag)
        {
            // prepare
            var left  = new ToastId(id, tag);
            var right = new ToastId(id, tag);

            // act & verify
            Assert.Equal(left, right);
            Assert.Equal(left.GetHashCode(), right.GetHashCode());
        }
Exemplo n.º 14
0
 Request?GetRequest(ToastId toastId)
 {
     lock (requests)
     {
         if (requests.TryGetValue(toastId, out var value))
         {
             return(value);
         }
     }
     return(null);
 }
 TaskCompletionSource <NotificationResult>?PopTask(ToastId toastId)
 {
     lock (mutex)
     {
         if (tasksByNotificationId.Remove(toastId, out var tcs))
         {
             return(tcs);
         }
     }
     return(null);
 }
Exemplo n.º 16
0
 public void RemoveDelivered(ToastId toastId)
 {
     if (string.IsNullOrEmpty(toastId.Group) == false)
     {
         ToastNotificationManager.History.Remove(toastId.Tag, toastId.Group);
     }
     else
     {
         ToastNotificationManager.History.Remove(toastId.Tag);
     }
 }
Exemplo n.º 17
0
        public void FromScheduledToastNotification()
        {
            var tn = new ScheduledToastNotification(new XmlDocument(), DateTimeOffset.Now + TimeSpan.FromDays(1))
            {
                Tag   = Guid.NewGuid().ToString(),
                Group = Guid.NewGuid().ToString(),
            };

            var toastId = ToastId.FromNotification(tn);

            Assert.Equal(tn.Tag, toastId.Tag);
            Assert.Equal(tn.Group, toastId.Group);
        }
        public IScheduledToastCancellation ScheduleTo(DateTimeOffset deliveryTime)
        {
            var tid = ToastId.New();
            var @do = CalculateDeliveryOffset(deliveryTime);
            var pi  = intentManager.RegisterToShowWithDelay(notificationBuilder, tid);

            var am = AlarmManager.FromContext(Application.Context)
                     ?? throw new InvalidOperationException(ErrorStrings.KAlarmManagerError);

            am.Set(AlarmType.ElapsedRealtimeWakeup, @do, pi);

            return(new AlarmCancellation(pi, tid));
        }
Exemplo n.º 19
0
        public void FromToastNotification()
        {
            var tn = new ToastNotification(new XmlDocument())
            {
                Tag   = Guid.NewGuid().ToString(),
                Group = Guid.NewGuid().ToString(),
            };

            var toastId = ToastId.FromNotification(tn);

            Assert.Equal(tn.Tag, toastId.Tag);
            Assert.Equal(tn.Group, toastId.Group);
        }
        PendingIntent CreateLaunchIntent(IPlatformNotificationBuilder builder, ToastId toastId)
        {
            var packageManager = Application.Context.PackageManager;
            var intent         = packageManager?.GetLaunchIntentForPackage(options.PackageName);

            if (intent == null)
            {
                throw new InvalidOperationException("can't get launch intent");
            }
            intent.AddFlags(ActivityFlags.NewTask | ActivityFlags.ClearTop);
            toastId.ToIntent(intent);
            builder.AddCustomArgsTo(intent);
            var result = PendingIntent.GetActivity(Application.Context, toastId.GetPersistentHashCode(), intent, PendingIntentFlags.UpdateCurrent)
                         ?? throw new InvalidOperationException(ErrorStrings.KBroadcastError);

            return(result);
        }
            private void OnTapped(ToastId toastId, TaskCompletionSource <NotificationResult>?tcs)
            {
                var activity = hrIntentManager.Value.options.Activity;

                if (activity.IsDestroyed)
                {
                    try
                    {
                        var packageManager = Application.Context.PackageManager;
                        var intent         = packageManager?.GetLaunchIntentForPackage(hrIntentManager.Value.options.PackageName);
                        if (intent != null)
                        {
                            intent.AddCategory(Intent.CategoryLauncher);
                            toastId.ToIntent(intent);
                            Application.Context.StartActivity(intent);
                        }
                    }
                    catch (Exception ex)
                    {
                        hrIntentManager.Value.logger?.LogError(ex, "can't start application activity");
                    }
                }
                else if (activity.HasWindowFocus == false)
                {
                    try
                    {
                        var intent = new Intent(activity, activity.GetType());
                        toastId.ToIntent(intent);
                        intent.SetFlags(ActivityFlags.ReorderToFront);
                        activity.StartActivityIfNeeded(intent, 0);
                    }
                    catch (Exception ex)
                    {
                        hrIntentManager.Value.logger?.LogError(ex, "can't reorder to front an application activity");
                    }
                }
                tcs?.TrySetResult(NotificationResult.Activated);
                hrIntentManager.Value.systemEventSource.SendEvent(new NotificationEvent(toastId));
            }
        async Task <NotificationResult> PrivateShowAsync(ToastId toastId, CancellationToken cancellationToken)
        {
            var tcs          = intentManager.RegisterToShowImmediatly(notificationBuilder, toastId);
            var notification = notificationBuilder.Build();
            var anm          = ANotificationManager.FromContext(Application.Context)
                               ?? throw new InvalidOperationException(ErrorStrings.KNotificationManagerError);

            using var timer = notificationBuilder.Timeout == Timeout.InfiniteTimeSpan ? null : new Timer(_ =>
            {
                if (notificationBuilder.CleanupOnTimeout)
                {
                    androidNotificationManager.Cancel(toastId);
                }
                tcs.TrySetResult(NotificationResult.TimedOut);
            }, null, notificationBuilder.Timeout, Timeout.InfiniteTimeSpan);
            androidNotificationManager.Notify(notification, toastId);
            if (cancellationToken.CanBeCanceled)
            {
                return(await tcs.WatchCancellationAsync(cancellationToken, () => androidNotificationManager.Cancel(toastId)));
            }
            return(await tcs.Task);
        }
        public Task <NotificationResult> ShowAsync(out ToastId toastId, CancellationToken cancellationToken)
        {
            var tcs      = new TaskCompletionSource <NotificationResult>();
            var view     = options.Activity.FindViewById(global::Android.Resource.Id.Content);
            var snackbar = Snackbar.Make(view, snackbarBuilder.Text, snackbarBuilder.SnackbarDuration);

            if (snackbarBuilder.ActionText != null)
            {
                snackbar.SetAction(snackbarBuilder.ActionText, v => tcs.TrySetResult(NotificationResult.Activated));
            }
            if (snackbarBuilder.ActionTextColor != null)
            {
                snackbar.SetActionTextColor(snackbarBuilder.ActionTextColor.Value);
            }
            snackbar.AddCallback(new Callback(tcs));
            snackbar.Show();
            toastId = ToastId.New();
            if (cancellationToken.CanBeCanceled)
            {
                return(tcs.WatchCancellationAsync(cancellationToken, () => snackbar.Dismiss()));
            }
            return(tcs.Task);
        }
Exemplo n.º 24
0
 public Task <bool> IsDeliveredAsync(ToastId toastId)
 => Task.FromResult(androidNotificationManager.IsDelivered(toastId));
Exemplo n.º 25
0
 public bool IsDelivered(ToastId toastId)
 => AndroidNotificationManager.NotificationManager.GetActiveNotifications()
 ?.Where(n => n.Tag == toastId.Tag && n.Id == toastId.Id)?.Any() == true;
Exemplo n.º 26
0
        public void OperatorNotEquals(bool expected, ToastId left, ToastId right)
        {
            var actual = (left != right);

            Assert.NotEqual(expected, actual);
        }
Exemplo n.º 27
0
 public Task <bool> IsScheduledAsync(ToastId toastId)
 => Task.FromResult(intentManager.IsPendingIntentExists(toastId));
Exemplo n.º 28
0
 public void RemoveDelivered(ToastId toastId)
 => androidNotificationManager.Cancel(toastId);
Exemplo n.º 29
0
 public Task <bool> IsDeliveredAsync(ToastId toastId)
 => Task.FromResult(ToastNotificationManager.History.GetHistory()
                    .Where(n => n.Tag == toastId.Tag && n.Group == toastId.Group).Any());
Exemplo n.º 30
0
 public Task <bool> IsScheduledAsync(ToastId toastId)
 {
     return(Task.FromResult(notifier.GetScheduledToastNotifications().Where(n => n.Tag == toastId.Tag && n.Group == toastId.Group).Any()));
 }