Example #1
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);
        }
        public void SetupNotifyWork(string title, string message, DateTime date, string name)
        {
            var data = new Data.Builder();

            data.PutString("Type", "Notify");
            data.PutString("Title", title);
            data.PutString("Message", message);

            var work = OneTimeWorkRequest.Builder.From <NotificationWorkManager>()
                       .SetInitialDelay(date.Subtract(DateTime.Now))
                       .SetInputData(data.Build())
                       .Build();

            WorkManager.GetInstance(AndroidApp.Context).EnqueueUniqueWork(name, ExistingWorkPolicy.Keep, work);
        }
        private void ShowLater(NotificationRequest notificationRequest)
        {
            if (notificationRequest.NotifyTime.HasValue == false)
            {
                return;
            }

            Cancel(notificationRequest.NotificationId);

            var triggerTime = NotifyTimeInMilliseconds(notificationRequest.NotifyTime.Value);

            notificationRequest.NotifyTime = null;
            triggerTime -= NotifyTimeInMilliseconds(DateTime.Now);

            var serializedNotification = ObjectSerializer <NotificationRequest> .SerializeObject(notificationRequest);

            var dataBuilder = new Data.Builder();

            dataBuilder.PutString(NotificationCenter.ExtraReturnNotification, serializedNotification);

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

            reqbuilder.AddTag(notificationRequest.NotificationId.ToString());
            reqbuilder.SetInputData(dataBuilder.Build());
            reqbuilder.SetInitialDelay(triggerTime, TimeUnit.Milliseconds);

            var workRequest = reqbuilder.Build();

            WorkManager.Instance.Enqueue(workRequest);
        }
Example #4
0
        private void ShowLater(NotificationRequest notificationRequest)
        {
            if (notificationRequest.NotifyTime.HasValue == false ||
                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 serializer             = new ObjectSerializer <NotificationRequest>();
            var serializedNotification = serializer.SerializeObject(notificationRequest);

            using (var dataBuilder = new Data.Builder())
            {
                dataBuilder.PutString(NotificationCenter.ExtraReturnNotification, serializedNotification);

                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.Instance.Enqueue(workRequest);
            }
        }
        public override void OnMessageReceived(RemoteMessage message)
        {
            //Log.Debug(TAG, "From: " + message.From);
            //var body = message.GetNotification().Body;
            //Log.Debug(TAG, "Notification Message Body: " + body);
            Log.Info(TAG, $"Received a firebase request from {message.From}.");
            if (message.From == $"/topics/{MainActivity.CHANNEL_ID}")
            {
                Log.Debug(TAG, "Received an announcement.");
                SendNotification(message.GetNotification().Body, message.GetNotification().Title, message.Data);
            }
            else if (message.From == $"/topics/{MainActivity.UPDATE_CHANNEL_NAME}" &&
                     !Preferences.ContainsKey("DisableAutoUpdate"))
            {
                Log.Debug(TAG, "Received an update request.");

                if (!message.Data.ContainsKey("region") || !int.TryParse(message.Data["region"], out var region))
                {
                    Log.Warn(TAG, "Firebase message missing region.");
                    return;
                }

                if (region < 1 || region > 2)
                {
                    Log.Warn(TAG, "Invalid region for update request.");
                    return;
                }

                string preferencesKey = "";
                switch (region)
                {
                case 1:
                    preferencesKey = $"InstalledScript_{FGORegion.Jp}";
                    break;

                case 2:
                    preferencesKey = $"InstalledScript_{FGORegion.Na}";
                    break;
                }

                if (Preferences.Get(preferencesKey, null) == null)
                {
                    Log.Warn(TAG, "User hasn't installed any script for this region.");
                    return;
                }

                var data = new Data.Builder();
                data.PutInt("region", region);
                data.PutString("preferencesKey", preferencesKey);
                var finalData = data.Build();
                var builder   = OneTimeWorkRequest.Builder.From <RayshiftTranslationUpdateWorker>();
                builder.SetInputData(finalData);

                OneTimeWorkRequest request = builder.Build();
                WorkManager.Instance.Enqueue(request);
            }
        }
Example #6
0
        public async Task <string> StartUploadForIdAsync(Guid id)
        {
            Data.Builder data = new Data.Builder();
            data.PutString("id", id.ToString());
            OneTimeWorkRequest oneTimeWorkRequest = OneTimeWorkRequest.Builder.From <RandomUploadWorker>()
                                                    .SetInputData(data.Build())
                                                    .Build();

            WorkManager.Instance.Enqueue(oneTimeWorkRequest);

            ILifecycleOwner owner = ProcessLifecycleOwner.Get();

            //Observe what happened to this request so we can notify the UI
            WorkManager.Instance.GetWorkInfoByIdLiveData(oneTimeWorkRequest.Id).Observe(owner, new RandomWorkerObserver());
            return(oneTimeWorkRequest.Id.ToString());
        }
Example #7
0
        protected override void RegisterNative(JobInfo jobInfo)
        {
            this.CancelNative(jobInfo);

            //WorkManager.Initialize(this.context.AppContext, new Configuration())
            var constraints = new Constraints.Builder()
                              .SetRequiresBatteryNotLow(jobInfo.BatteryNotLow)
                              .SetRequiresCharging(jobInfo.DeviceCharging)
                              .SetRequiredNetworkType(ToNative(jobInfo.RequiredInternetAccess))
                              .Build();

            var data = new Data.Builder();

            data.PutString(ShinyJobWorker.ShinyJobIdentifier, jobInfo.Identifier);

            if (jobInfo.Repeat)
            {
                var request = new PeriodicWorkRequest.Builder(typeof(ShinyJobWorker), TimeSpan.FromMinutes(15))
                              .SetConstraints(constraints)
                              .SetInputData(data.Build())
                              .Build();

                this.Instance.EnqueueUniquePeriodicWork(
                    jobInfo.Identifier,
                    ExistingPeriodicWorkPolicy.Replace,
                    request
                    );
            }
            else
            {
                var worker = new OneTimeWorkRequest.Builder(typeof(ShinyJobWorker))
                             .SetInputData(data.Build())
                             .SetConstraints(constraints)
                             .Build();

                this.Instance.EnqueueUniqueWork(
                    jobInfo.Identifier,
                    ExistingWorkPolicy.Append,
                    worker
                    );
            }
        }