示例#1
0
        public async Task <ResponseNotifyModel> SendNotify(string applicationToken,
                                                           RequestNotifyModel requestModel)
        {
            var application = _applicationDataUnit.GetApplication(applicationToken);

            if (application == null)
            {
                throw new ArgumentNullException("application is null");
            }

            // { notification = { title = string, body = string, badge = 0, sound = default }, data = { actionType = 4 } }
            var payload = new
            {
                notification = new
                {
                    title = requestModel.Title,
                    body  = requestModel.Body,
                    badge = requestModel.Badge,
                    sound = requestModel.Sound
                },
            };

            var jsonObject = JObject.FromObject(payload);
            var customData = new JObject();

            if (requestModel.CustomData != null && requestModel.CustomData.Any())
            {
                foreach (var(key, value) in requestModel.CustomData)
                {
                    var jsonElement = (JsonElement)value;
                    switch (jsonElement.ValueKind)
                    {
                    case JsonValueKind.Number:
                        customData.Add(key, jsonElement.GetInt32());
                        break;

                    case JsonValueKind.String:
                        customData.Add(key, jsonElement.GetString());
                        break;

                    case JsonValueKind.True:
                        customData.Add(key, jsonElement.GetBoolean());
                        break;

                    case JsonValueKind.False:
                        customData.Add(key, jsonElement.GetBoolean());
                        break;
                    }
                }

                jsonObject.Add("data", customData);
            }

            var firebaseResult =
                await _firebaseNotificationSender.SendAsync(application.ServerKey, application.SenderId,
                                                            requestModel.DeviceIds,
                                                            jsonObject);

            return(_mapper.Map <ResponseNotifyModel>(firebaseResult));
        }
示例#2
0
        public async Task <IActionResult> SendNotify([FromBody] RequestNotifyModel requestNotifyModel)
        {
            var responseNotifyModel =
                await _firebaseNotificationService.SendNotify(Request.Headers.GetApplicationToken(),
                                                              requestNotifyModel);

            return(Ok(responseNotifyModel));
        }