Exemplo n.º 1
0
        public async Task <IActionResult> SendPushNotificationEventSample(SendPushNotificationModel model)
        {
            var pushNotificationEvent = new SendPushNotificationEvent()
            {
                ApplicationToken = model.ApplicationToken,
                Title            = model.Title,
                Body             = model.Body,
                DeviceIds        = model.DeviceIds,
                CustomData       = model.CustomData,
                Badge            = model.Badge
            };

            await HelperEvent.PublishEvent(_publishEndpoint, pushNotificationEvent, _logger);

            return(Ok("Success"));
        }
        public IRestResponse SendPushNotification(SendPushNotificationModel pushNotification, Authenticator authenticator = null)
        {
            if (ReferenceEquals(null, pushNotification) == true)
            {
                throw new ArgumentNullException(nameof(pushNotification));
            }

            const string resource = "PushNotifications/Send";

            var request = CreateRestRequest(resource, Method.POST, authenticator)
                          .AddJsonBody(pushNotification);

            var response = CreateRestClient().Execute <ResponseResult>(request);

            return(response);
        }
Exemplo n.º 3
0
        public async Task <SendPushNotificationResultModel> SendPushNotificationAsync(SendPushNotificationModel model)
        {
            var result = new SendPushNotificationResultModel();

            // Try to send push notification via Infobip client and propagate proper audit message
            try
            {
                _log.Info("Sending push notification with Infobip...", new { model.MessageId });

                var response =
                    await _infobipClient.Api.SendPushNotificationAsync(new SendPushNotificationRequestModel
                {
                    From = _fromSender,
                    To   = new RecipientDestinationAddressTypeModel
                    {
                        PushRegistrationId = model.PushRegistrationId
                    },
                    Text          = model.Message,
                    CustomPayload = model.CustomPayload?.Count == 0 ? new JObject() : JObject.Parse(JsonConvert.SerializeObject(model.CustomPayload, Formatting.Indented))
                });

                // Valid statuses that are treated as push notification is sent
                // 0 - OK, 1 - PENDING, 3 - DELIVERED
                var validStatuses = new[] { "OK", "PENDING", "DELIVERED" };
                var errorMessage  = "";

                foreach (var bulk in response.Bulks)
                {
                    if (validStatuses.All(x =>
                                          string.Compare(x, bulk.Status.GroupName, StringComparison.InvariantCultureIgnoreCase) != 0))
                    {
                        _log.Error(
                            null,
                            "Could not send push notification",
                            new { model.MessageId, InfobipResponse = new { bulk.BulkId, bulk.Status } });

                        if (string.IsNullOrEmpty(errorMessage))
                        {
                            errorMessage += $"{bulk.Status.Name} - {bulk.Status.Description}";
                        }
                        else
                        {
                            errorMessage += $"{Environment.NewLine}{bulk.Status.Name} - {bulk.Status.Description}";
                        }
                    }
                }

                if (!string.IsNullOrEmpty(errorMessage))
                {
                    result.Result       = ResultCodeModel.Error;
                    result.ErrorMessage = errorMessage;

                    return(result);
                }

                result.Result = ResultCodeModel.Ok;

                _log.Info("Push notification sent", new { model.MessageId });
            }
            catch (Exception e)
            {
                _log.Error(e, "Could not send push notification", new { model.MessageId });

                result.Result       = ResultCodeModel.Error;
                result.ErrorMessage = e.Message;
            }

            return(result);
        }