Exemplo n.º 1
0
        /// <summary>
        /// Publish a push notification message.
        /// </summary>
        /// <param name="recipient">Recipient. Best description of what is allowed can be found in the RestApi documentation: https://ably.com/docs/rest-api#post-device-registration.</param>
        /// <param name="payload">Message payload.</param>
        /// <returns>Task.</returns>
        public async Task PublishAsync(JObject recipient, JObject payload)
        {
            ValidateRequest();
            var request = _restClient.CreatePostRequest("/push/publish");

            AddFullWaitIfNecessary(request);
            var data = new JObject {
                { "recipient", recipient }
            };

            foreach (var property in payload.Properties())
            {
                data.Add(property.Name, property.Value);
            }

            request.PostData = data;

            _ = await _restClient.ExecuteRequest(request);

            void ValidateRequest()
            {
                if (recipient is null)
                {
                    throw new AblyException("Please provide a valid and non-empty recipient", ErrorCodes.BadRequest);
                }

                if (payload is null)
                {
                    throw new AblyException("Please provide a non-empty payload", ErrorCodes.BadRequest);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Publish a message to the channel
        /// </summary>
        /// <param name="name">The event name of the message to publish</param>
        /// <param name="data">The message payload. Allowed payloads are string, objects and byte[]</param>
        /// <param name="clientId">Explicit message clientId</param>
        public Task PublishAsync(string name, object data, string clientId = null)
        {
            var request = _ablyRest.CreatePostRequest(_basePath + "/messages", Options);

            request.PostData = new List <Message> {
                new Message(name, data, clientId)
            };
            return(_ablyRest.ExecuteRequest(request));
        }