Пример #1
0
        public NotificationManager(AndroidContext context,
                                   IServiceProvider services,
                                   ISerializer serializer,
                                   IJobManager jobs,
                                   IRepository repository,
                                   ISettings settings)
        {
            this.context    = context;
            this.services   = services;
            this.serializer = serializer;
            this.jobs       = jobs;
            this.repository = repository;
            this.settings   = settings;

            // auto process intent?
            //this.context
            //    .WhenActivityStatusChanged()
            //    .Where(x => x.Status == ActivityState.Created)
            //    .Subscribe(x => TryProcessIntent(x.Activity.Intent));

            if (this.context.IsMinApiLevel(26))
            {
                this.newManager = Native.FromContext(context.AppContext);
            }
            else
            {
                this.compatManager = NotificationManagerCompat.From(context.AppContext);
            }
        }
        public ANotificationChannel Build()
        {
            if (name == null)
            {
                throw new InvalidOperationException("name required");
            }
            if (id == null)
            {
                id = GenerateIdByName(name);
            }
            var nm = ANotificationManager.FromContext(Application.Context)
                     ?? throw new InvalidOperationException(ErrorStrings.KNotificationManagerError);
            var channel = nm.GetNotificationChannel(id);

            if (channel != null)
            {
                return(channel);
            }
            ANotificationImportance ni = this.notificationImportance != null
                ? (ANotificationImportance)(int)this.notificationImportance
                : ANotificationImportance.Default;
            var anc = new ANotificationChannel(id, name, ni);

            if (description != null)
            {
                anc.Description = description;
            }
            Apply(lights, anc.EnableLights);
            Apply(vibration, anc.EnableVibration);
#if __ANDROID_29__
            Apply(allowBubbles, anc.SetAllowBubbles);
#endif
            Apply(bypassDnd, anc.SetBypassDnd);
            if (group != null)
            {
                anc.Group = group;
            }
            Apply(showBadge, anc.SetShowBadge);
            if (vibrationPattern != null)
            {
                anc.SetVibrationPattern(vibrationPattern);
            }
            if (sound != null && audioAttributes != null)
            {
                anc.SetSound(sound, audioAttributes);
            }
            nm.CreateNotificationChannel(anc);
            return(anc);
        }
Пример #3
0
        public NotificationManager(AndroidContext context,
                                   IJobManager jobs,
                                   IRepository repository,
                                   ISettings settings)
        {
            this.context    = context;
            this.jobs       = jobs;
            this.repository = repository;
            this.settings   = settings;

            if ((int)Build.VERSION.SdkInt >= 26)
            {
                this.newManager = Native.FromContext(context.AppContext);
            }
            else
            {
                this.compatManager = NotificationManagerCompat.From(context.AppContext);
            }
        }
Пример #4
0
        private void SendNotification(PushNotification notificationDetails)
        {
            var notificationManager = NotificationManager.FromContext(this);

            var channelId = Covi.Configuration.Constants.PushNotificationsConstants.NotificationChannelName;

            var notificationId      = new Random().Next();
            var largeIcon           = BitmapFactory.DecodeResource(Resources, Resource.Mipmap.icon);
            var notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                .SetSmallIcon(Resource.Drawable.notification_icon)
                .SetLargeIcon(largeIcon)
                .SetPriority(NotificationCompat.PriorityHigh)
                .SetContentIntent(BuildIntentToShowMainActivity())
                .SetAutoCancel(true);

            if (!string.IsNullOrEmpty(notificationDetails.Title))
            {
                notificationBuilder.SetContentTitle(notificationDetails.Title);
            }

            if (!string.IsNullOrEmpty(notificationDetails.SubTitle))
            {
                notificationBuilder.SetSubText(notificationDetails.SubTitle);
            }

            if (!string.IsNullOrEmpty(notificationDetails.Description))
            {
                notificationBuilder.SetContentText(notificationDetails.Description);
                notificationBuilder.SetStyle(new NotificationCompat.BigTextStyle().BigText(notificationDetails.Description));
            }

            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                notificationBuilder.SetChannelId(Covi.Configuration.Constants.PushNotificationsConstants.NotificationChannelName);
            }

            notificationManager.Notify(notificationId, notificationBuilder.Build());
        }
        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);
        }