forked from grandnode/grandnode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushNotificationsService.cs
More file actions
247 lines (224 loc) · 10.1 KB
/
Copy pathPushNotificationsService.cs
File metadata and controls
247 lines (224 loc) · 10.1 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using Grand.Core;
using Grand.Core.Data;
using Grand.Core.Domain.PushNotifications;
using Grand.Services.Events;
using Grand.Services.Localization;
using Grand.Services.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using MediatR;
namespace Grand.Services.PushNotifications
{
public class PushNotificationsService : IPushNotificationsService
{
private readonly IRepository<PushRegistration> _pushRegistratiosnRepository;
private readonly IRepository<PushMessage> _pushMessagesRepository;
private readonly IMediator _mediator;
private readonly PushNotificationsSettings _pushNotificationsSettings;
private readonly ILocalizationService _localizationService;
private readonly ILogger _logger;
public PushNotificationsService(IRepository<PushRegistration> pushRegistratiosnRepository, IRepository<PushMessage> pushMessagesRepository,
IMediator mediator, PushNotificationsSettings pushNotificationsSettings, ILocalizationService localizationService, ILogger logger)
{
this._pushRegistratiosnRepository = pushRegistratiosnRepository;
this._pushMessagesRepository = pushMessagesRepository;
this._mediator = mediator;
this._pushNotificationsSettings = pushNotificationsSettings;
this._localizationService = localizationService;
this._logger = logger;
}
/// <summary>
/// Inserts push receiver
/// </summary>
/// <param name="model"></param>
public virtual async Task InsertPushReceiver(PushRegistration registration)
{
await _pushRegistratiosnRepository.InsertAsync(registration);
await _mediator.EntityInserted(registration);
}
/// <summary>
/// Deletes push receiver
/// </summary>
/// <param name="model"></param>
public virtual async Task DeletePushReceiver(PushRegistration registration)
{
await _pushRegistratiosnRepository.DeleteAsync(registration);
await _mediator.EntityDeleted(registration);
}
/// <summary>
/// Gets push receiver
/// </summary>
/// <param name="CustomerId"></param>
public virtual async Task<PushRegistration> GetPushReceiverByCustomerId(string CustomerId)
{
return await _pushRegistratiosnRepository.Table.Where(x => x.CustomerId == CustomerId).FirstOrDefaultAsync();
}
/// <summary>
/// Updates push receiver
/// </summary>
/// <param name="registration"></param>
public virtual async Task UpdatePushReceiver(PushRegistration registration)
{
await _pushRegistratiosnRepository.UpdateAsync(registration);
await _mediator.EntityUpdated(registration);
}
/// <summary>
/// Gets all push receivers
/// </summary>
public virtual async Task<List<PushRegistration>> GetPushReceivers()
{
return await _pushRegistratiosnRepository.Table.Where(x => x.Allowed).ToListAsync();
}
/// <summary>
/// Gets number of customers that accepted push notifications permission popup
/// </summary>
public virtual Task<int> GetAllowedReceivers()
{
return _pushRegistratiosnRepository.Table.Where(x => x.Allowed).CountAsync();
}
/// <summary>
/// Gets number of customers that denied push notifications permission popup
/// </summary>
public virtual Task<int> GetDeniedReceivers()
{
return _pushRegistratiosnRepository.Table.Where(x => !x.Allowed).CountAsync();
}
/// <summary>
/// Inserts push message
/// </summary>
/// <param name="registration"></param>
public virtual async Task InsertPushMessage(PushMessage message)
{
await _pushMessagesRepository.InsertAsync(message);
await _mediator.EntityInserted(message);
}
/// <summary>
/// Gets all push messages
/// </summary>
public virtual async Task<IPagedList<PushMessage>> GetPushMessages(int pageIndex = 0, int pageSize = int.MaxValue)
{
var allMessages = await _pushMessagesRepository.Table.OrderByDescending(x => x.SentOn).ToListAsync();
return new PagedList<PushMessage>(allMessages.Skip(pageIndex * pageSize).Take(pageSize).ToList(), pageIndex, pageSize, allMessages.Count);
}
/// <summary>
/// Gets all push receivers
/// </summary>
public virtual async Task<IPagedList<PushRegistration>> GetPushReceivers(int pageIndex = 0, int pageSize = int.MaxValue)
{
var allReceivers = await _pushRegistratiosnRepository.Table.OrderByDescending(x => x.RegisteredOn).ToListAsync();
return new PagedList<PushRegistration>(allReceivers.Skip(pageIndex * pageSize).Take(pageSize).ToList(), pageIndex, pageSize, allReceivers.Count);
}
/// <summary>
/// Sends push notification to all receivers
/// </summary>
/// <param name="title"></param>
/// <param name="text"></param>
/// <param name="pictureUrl"></param>
/// <param name="registrationIds"></param>
/// <param name="clickUrl"></param>
/// <returns>Bool indicating whether message was sent successfully and string result to display</returns>
public virtual async Task<(bool, string)> SendPushNotification(string title, string text, string pictureUrl, string clickUrl, List<string> registrationIds = null)
{
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var ids = new List<string>();
if (registrationIds != null && registrationIds.Any())
{
ids = registrationIds;
}
else
{
var receivers = await GetPushReceivers();
if (!receivers.Any())
{
return (false, _localizationService.GetResource("Admin.PushNotifications.Error.NoReceivers"));
}
foreach (var receiver in receivers)
{
if (!ids.Contains(receiver.Token))
ids.Add(receiver.Token);
}
}
var data = new
{
registration_ids = ids,
notification = new
{
body = text,
title = title,
icon = pictureUrl,
click_action = clickUrl
}
};
var json = JsonConvert.SerializeObject(data);
Byte[] byteArray = Encoding.UTF8.GetBytes(json);
tRequest.Headers.Add(string.Format("Authorization: key={0}", _pushNotificationsSettings.PrivateApiKey));
tRequest.Headers.Add(string.Format("Sender: id={0}", _pushNotificationsSettings.SenderId));
tRequest.ContentLength = byteArray.Length;
try
{
using (Stream dataStream = tRequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
using (WebResponse tResponse = tRequest.GetResponse())
{
using (Stream dataStreamResponse = tResponse.GetResponseStream())
{
using (StreamReader tReader = new StreamReader(dataStreamResponse))
{
String sResponseFromServer = tReader.ReadToEnd();
var response = JsonConvert.DeserializeObject<JsonResponse>(sResponseFromServer);
if (response.failure > 0)
{
await _logger.InsertLog(Core.Domain.Logging.LogLevel.Error, "Error occured while sending push notification.", sResponseFromServer);
}
await InsertPushMessage(new PushMessage
{
NumberOfReceivers = response.success,
SentOn = DateTime.UtcNow,
Text = text,
Title = title
});
return (true, string.Format(_localizationService.GetResource("Admin.PushNotifications.MessageSent"), response.success, response.failure));
}
}
}
}
}
catch (Exception ex)
{
return (false, ex.Message);
}
}
/// <summary>
/// Sends push notification to specified customer
/// </summary>
/// <param name="title"></param>
/// <param name="text"></param>
/// <param name="pictureUrl"></param>
/// <param name="customerId"></param>
/// <param name="clickUrl"></param>
/// <returns>Bool indicating whether message was sent successfully and string result to display</returns>
public virtual async Task<(bool, string)> SendPushNotification(string title, string text, string pictureUrl, string customerId, string clickUrl)
{
return await SendPushNotification(title, text, pictureUrl, clickUrl, new List<string> { GetPushReceiverByCustomerId(customerId).Id.ToString() });
}
/// <summary>
/// Gets all push receivers
/// </summary>
/// <param name="Id"></param>
public virtual Task<PushRegistration> GetPushReceiver(string Id)
{
return _pushRegistratiosnRepository.Table.Where(x => x.Id == Id).FirstOrDefaultAsync();
}
}
}