Example #1
0
        public async Task <Result> SendMessage(PushMessageInput input)
        {
            var result = await SendWithFcm(input);

            if (result.MessageResponse.Results == null)
            {
                result.MessageResponse.Results = new List <FCM.Net.Result>();
            }


            return(new Result(result.StatusCode.ToString(), result.MessageResponse.Failure, result.MessageResponse.Results.Select(a => a.Error).ToList(), result.ReasonPhrase));
        }
Example #2
0
        async Task <ResponseContent> SendWithFcm(PushMessageInput input)
        {
            using (var sender = new Sender(FirebaseServerKey))
            {
                var message = new Message
                {
                    RegistrationIds = input.Players,
                    Notification    = new Notification
                    {
                        Title = input.Title,
                        Body  = input.Desc,
                        Sound = "default"
                    },
                    Data     = input.Data,
                    Priority = Priority.High,
                };
                var response = await sender.SendAsync(message);

                return(response);
            }
        }
Example #3
0
        protected override void DoWork()
        {
            var notifications = _phoneNotificationsRepository.GetAllIncluding(a => a.SendNotificationsStatuses);

            var notificationsOnThisDay = notifications.Where(a => a.NotifyDate.Month == DateTime.Now.Month &&
                                                             a.NotifyDate.Year == DateTime.Now.Year &&
                                                             a.NotifyDate.Day <= DateTime.Now.Day &&
                                                             a.NotifyDate.Hour <= DateTime.Now.Hour &&
                                                             a.NotifyDate.Minute <= DateTime.Now.Minute).ToList();

            var serializer = new JavaScriptSerializer();

            foreach (var phoneNotification in notificationsOnThisDay)
            {
                foreach (var phoneNotificationSendNotificationsStatus
                         in phoneNotification.SendNotificationsStatuses
                         .Where(a => a.ResultContent == null && !a.Sent && !a.SendTried))
                {
                    var message = new PushMessageInput()
                    {
                        Desc    = phoneNotification.Message,
                        Title   = phoneNotification.Title,
                        Players = new List <string>()
                        {
                            phoneNotificationSendNotificationsStatus.Token
                        },
                        Data = serializer.Deserialize <DataMessageRequest>(phoneNotification.Data)
                    };
                    var sendResult = AsyncHelper.RunSync(() => _pushManager.SendMessage(message));

                    if (sendResult.Failure <= 0 && sendResult.Code != "BadRequest")
                    {
                        phoneNotificationSendNotificationsStatus.Sent = true;
                    }
                    phoneNotificationSendNotificationsStatus.SendTried     = true;
                    phoneNotificationSendNotificationsStatus.ResultContent = $"Estado http: {sendResult.Code} - Resultado: {sendResult.Response.FirstOrDefault()}";
                }
            }
        }