/// <summary>
        ///
        /// </summary>
        /// <param name="notificationRequest"></param>
        protected internal virtual void EnqueueWorker(NotificationRequest notificationRequest)
        {
            if (!notificationRequest.NotifyTime.HasValue)
            {
                Log($"{nameof(notificationRequest.NotifyTime)} value doesn't set!");
                return;
            }

            var notifyTime             = notificationRequest.NotifyTime.Value;
            var serializedNotification = ObjectSerializer.SerializeObject(notificationRequest);

            // Why serialized options separately ?
            // System.Xml.Serialization.XmlSerializer Deserialize and Serialize methods ignore
            // object property "Android" when linking option set to "SDK Assemblies Only"
            var serializedNotificationAndroid = ObjectSerializer.SerializeObject(notificationRequest.Android);

            Log($"NotificationServiceImpl.ShowLater: SerializedNotification [{serializedNotification}]");

            using var dataBuilder = new Data.Builder()
                                    .PutString(NotificationCenter.ExtraReturnNotification, serializedNotification)
                                    .PutString($"{NotificationCenter.ExtraReturnNotification}_Android", serializedNotificationAndroid);
            var data = dataBuilder.Build();
            var tag  = notificationRequest.NotificationId.ToString(CultureInfo.CurrentCulture);
            var diff = (long)(notifyTime - DateTime.Now).TotalMilliseconds;

            var workRequest = OneTimeWorkRequest.Builder.From <ScheduledNotificationWorker>()
                              .AddTag(tag)
                              .SetInputData(data)
                              .SetInitialDelay(diff, TimeUnit.Milliseconds)
                              .Build();

            WorkManager?.Enqueue(workRequest);
        }
Пример #2
0
        private void ShowLater(NotificationRequest notificationRequest)
        {
            if (notificationRequest.NotifyTime is null ||
                notificationRequest.NotifyTime.Value <= DateTime.Now) // To be consistent with iOS, Do not Schedule notification if NotifyTime is earlier than DateTime.Now
            {
                return;
            }

            Cancel(notificationRequest.NotificationId);

            var notifyTime             = notificationRequest.NotifyTime.Value;
            var serializedNotification = ObjectSerializer.SerializeObject(notificationRequest);
            // Why serialized options separately ?
            // System.Xml.Serialization.XmlSerializer Deserialize and Serialize methods ignore object property "Android" when linking option set to "SDK Assemblies Only"
            var serializedNotificationAndroid = ObjectSerializer.SerializeObject(notificationRequest.Android);

            Log.Info(Application.Context.PackageName, $"NotificationServiceImpl.ShowLater: SerializedNotification [{serializedNotification}]");
            using var dataBuilder = new Data.Builder();
            dataBuilder.PutString(NotificationCenter.ExtraReturnNotification, serializedNotification);
            dataBuilder.PutString($"{NotificationCenter.ExtraReturnNotification}_Android", serializedNotificationAndroid);

            var requestBuilder = OneTimeWorkRequest.Builder.From <ScheduledNotificationWorker>();

            requestBuilder.AddTag(notificationRequest.NotificationId.ToString(CultureInfo.CurrentCulture));
            requestBuilder.SetInputData(dataBuilder.Build());
            var diff = (long)(notifyTime - DateTime.Now).TotalMilliseconds;

            requestBuilder.SetInitialDelay(diff, TimeUnit.Milliseconds);

            var workRequest = requestBuilder.Build();

            _workManager?.Enqueue(workRequest);
        }
Пример #3
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            workerManager = WorkManager.GetInstance(Application.Context);
            workerManager.CancelAllWork();
            workerManager.PruneWork();
            SetContentView(Resource.Layout.activity_main);

            progressBar = FindViewById <ProgressBar>(Resource.Id.progressBar1);
            textView    = FindViewById <TextView>(Resource.Id.textView1);
            Button createWork = FindViewById <Button>(Resource.Id.button1);

            createWork.Click += (sender, e) =>
            {
                textView.Text = string.Empty;
                var oneTimeWorkRequest = new OneTimeWorkRequest.Builder(typeof(MyWorker))
                                         .AddTag("MyWorker")
                                         .SetInitialDelay(TimeSpan.FromSeconds(10))
                                         .Build();

                workerManager.BeginUniqueWork("MyWorker", ExistingWorkPolicy.Replace, oneTimeWorkRequest).Enqueue();
            };

            Button createListenableWork = FindViewById <Button>(Resource.Id.button2);

            createListenableWork.Click += (sender, e) =>
            {
                textView.Text = string.Empty;
                var periodicWorkRequest =
                    new PeriodicWorkRequest.Builder(typeof(MyListenableWorker), TimeSpan.FromMinutes(10))
                    .SetBackoffCriteria(BackoffPolicy.Linear, TimeSpan.FromSeconds(10))
                    .AddTag("MyListenableWorker")
                    .Build();

                workerManager.Enqueue(periodicWorkRequest);
                workerManager.GetWorkInfosByTagLiveData("MyListenableWorker").Observe(this, this);
            };

            Button cancelListenableWork = FindViewById <Button>(Resource.Id.button3);

            cancelListenableWork.Click += (sender, e) =>
            {
                workerManager.CancelAllWorkByTag("MyListenableWorker");
            };
        }
Пример #4
0
        public override void OnReceive(Context context, Intent intent)
        {
            _loggerService.StartMethod();

            if (intent.Action != Intent.ActionMyPackageReplaced)
            {
                _loggerService.EndMethod();
                return;
            }

            WorkManager workManager = WorkManager.GetInstance(context);
            var         worker      = new OneTimeWorkRequest.Builder(
                Java.Lang.Class.FromType(typeof(UpgradeWorker))
                ).Build();

            _ = workManager.Enqueue(worker);

            _loggerService.EndMethod();
        }