public async static Task <bool> SendOperationCompleteNotificationAsync(Response responseObj)
        {
            NotificationMessageBody fcmMessageBody = new NotificationMessageBody();;
            bool respValue = false;
            HttpResponseMessage httpResponseMessage = null;

            HttpClient httpClient = new HttpClient();

            httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            switch (responseObj.FlowMap.NotificationType)
            {
            case NotificationType.Targeted:
                var userMessageRequest = new UserMessageRequest();
                userMessageRequest.UserId     = responseObj.UserId;
                userMessageRequest.ResponseId = responseObj._id;

                userMessageRequest.MessageContent       = new MessageContent();
                userMessageRequest.MessageContent.title = responseObj.FlowMap.CMSOperation + " complete";
                fcmMessageBody.NotificationType         = NotificationType.Targeted;
                fcmMessageBody.NotificationTopic        = NotificationTopic.NA;
                fcmMessageBody.ResponseId              = responseObj._id;
                fcmMessageBody.CMSOperation            = responseObj.FlowMap.CMSOperation;
                userMessageRequest.MessageContent.body = fcmMessageBody;
                httpResponseMessage = await httpClient.PostJsonAsync <UserMessageRequest>(NotificationReceiverConfigSettings.UrlSendMessageToUser, userMessageRequest);

                if (httpResponseMessage.IsSuccessStatusCode)
                {
                    respValue = true;
                }
                break;

            case NotificationType.Broadcast:
                var topicMessageRequest = new TopicMessageRequest();
                topicMessageRequest.ResponseId   = responseObj._id;
                topicMessageRequest.TopicPattern = responseObj.FlowMap.NotificationTopic;

                topicMessageRequest.MessageContent       = new MessageContent();
                topicMessageRequest.MessageContent.title = responseObj.FlowMap.CMSOperation + " complete";
                fcmMessageBody.NotificationType          = NotificationType.Broadcast;
                fcmMessageBody.NotificationTopic         = responseObj.FlowMap.NotificationTopic;
                fcmMessageBody.ResponseId               = responseObj._id;
                fcmMessageBody.CMSOperation             = responseObj.FlowMap.CMSOperation;
                topicMessageRequest.MessageContent.body = fcmMessageBody;
                httpResponseMessage = await httpClient.PostJsonAsync <TopicMessageRequest>(NotificationReceiverConfigSettings.UrlSendMessageToTopic, topicMessageRequest);

                if (httpResponseMessage.IsSuccessStatusCode)
                {
                    respValue = true;
                }
                break;
            }

            return(respValue);
        }
Example #2
0
        public async Task <bool> SendMessageToTopicAsync(TopicMessageRequest message)
        {
            dynamic request = new ExpandoObject();

            request.to           = string.Format("/topics/{0}", message.TopicPattern);
            request.notification = message.MessageContent;

            var response = await this._httpClient.PostJsonAsync <ExpandoObject>(this._fcmAppSettings.FCMMessageSendingUrl, (ExpandoObject)request);

            //TODO:The Call will return you partial success,the app server should retry with back off between retries.
            // For Now only checking the Success of the call not the partial success.
            if (response.IsSuccessStatusCode)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }