-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValuesController.cs
More file actions
153 lines (134 loc) · 5.67 KB
/
Copy pathValuesController.cs
File metadata and controls
153 lines (134 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using Newtonsoft.Json;
using NLog;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using WebAPI.Models;
namespace WebAPI.Controllers
{
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
/// <summary>
/// Делаем web push уведомление через Firebase
/// </summary>
/// <returns>Возвращаем ответ типа string</returns>
[Route("api/Values/PostNotification")]
[HttpPost]
public async Task<string> PostNotification(Notification notifi)
{
Logger logger = LogManager.GetCurrentClassLogger();
logger.Info("Вызов PostNotification");
if (string.IsNullOrEmpty(notifi.Token) || string.IsNullOrEmpty(notifi.Body) || string.IsNullOrEmpty(notifi.Title))
{
logger.Error($"Заполните Token{0} body{1} title{2}", notifi.Token, notifi.Body, notifi.Title);
return "Заполните Token,body,title";
}
try
{
logger.Info("Попытка отправки уведомления");
var auth = ConfigurationManager.AppSettings["Authorization"];
var Sender = ConfigurationManager.AppSettings["Sender"];
var priority = ConfigurationManager.AppSettings["priority"];
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
//serverKey - Key from Firebase cloud messaging server
tRequest.Headers.Add(string.Format("Authorization: key={0}", auth));
//Sender Id - From firebase project setting
tRequest.Headers.Add(string.Format("Sender: id={0}", Sender));
tRequest.ContentType = "application/json";
var payload = new
{
to = notifi.Token,
priority = priority,
content_available = true,
notification = new
{
body = notifi.Body,
title = notifi.Title,
icon = notifi.Icon,
image = notifi.Image,
click_action = notifi.Url,
badge = 1
}
,
data = new
{
body = notifi.Body,
title = notifi.Title,
icon = notifi.Icon,
image = notifi.Image,
click_action = notifi.Url,
// badge = 1
}
};
string postbody = JsonConvert.SerializeObject(payload).ToString();
Byte[] byteArray = Encoding.UTF8.GetBytes(postbody);
tRequest.ContentLength = byteArray.Length;
using (Stream dataStream = await tRequest.GetRequestStreamAsync())
{
dataStream.Write(byteArray, 0, byteArray.Length);
using (WebResponse tResponse = await tRequest.GetResponseAsync())
{
using (Stream dataStreamResponse = tResponse.GetResponseStream())
{
if (dataStreamResponse != null) using (StreamReader tReader = new StreamReader(dataStreamResponse))
{
var sResponseFromServer = tReader.ReadToEnd();
var ResponceFirebase = JsonConvert.DeserializeObject<FirebaseJson>(sResponseFromServer);
if (ResponceFirebase.success == 1 && ResponceFirebase.failure == 0)
{
logger.Info("Увемдоление отправлено успешно: "+ postbody);
return "OK";
}
else
{
logger.Error("Ошибка отправки:" + ResponceFirebase.failure);
return "ResponceFirebase.success:" + ResponceFirebase.success + " ResponceFirebase.failure:" + ResponceFirebase.failure;
}
}
else
{
logger.Error("dataStreamResponse is null");
return "dataStreamResponse is null";
}
}
}
}
}
catch(Exception ex)
{
logger.Error("Exception: " +ex.InnerException.ToString());
return ex.Message.ToString();
}
}
// POST api/values
public void Post([FromBody]string value)
{
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
}