Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PushTokenRegistrar.cs
1using System;
3using System.Threading;
4using System.Threading.Tasks;
11
13{
18 {
19 private readonly IXmppService xmppService;
20 private const int MaxAttempts = 3;
21
26 public PushTokenRegistrar(IXmppService XmppService)
27 {
28 this.xmppService = XmppService;
29 }
30
38 public async Task<bool> ReportTokenAsync(TokenInformation TokenInformation, bool ForceReport, CancellationToken CancellationToken)
39 {
40 CancellationToken.ThrowIfCancellationRequested();
41
42 if (!ForceReport && !await ForceTokenReportAsync(TokenInformation))
43 return false;
44
45 string? Token = TokenInformation.Token;
46 if (string.IsNullOrEmpty(Token))
47 return false;
48
50 MaxAttempts,
51 (Attempt, _) => TimeSpan.FromSeconds(Math.Pow(2, Attempt - 1)),
52 (Attempt, ex, delay) => ServiceRef.LogService.LogWarning(
53 "Push token registration retry scheduled.",
54 new KeyValuePair<string, object?>("attempt", Attempt),
55 new KeyValuePair<string, object?>("delayMs", delay.TotalMilliseconds),
56 new KeyValuePair<string, object?>("exception", ex.Message)));
57
58 try
59 {
60 await PolicyRunner.RunAsync(
61 async _ =>
62 {
63 await this.xmppService.ReportNewPushNotificationToken(Token, TokenInformation.Service, TokenInformation.ClientType);
66 },
67 CancellationToken,
69 }
70 catch (Exception ex)
71 {
72 ServiceRef.LogService.LogInformational("Push token registration failed after retries.");
73 ServiceRef.LogService.LogException(
74 ex,
75 new KeyValuePair<string, object?>("service", TokenInformation.Service.ToString()),
76 new KeyValuePair<string, object?>("clientType", TokenInformation.ClientType.ToString()));
77 return false;
78 }
79
80 ServiceRef.LogService.LogInformational(
81 "Push token reported.",
82 new KeyValuePair<string, object?>("service", TokenInformation.Service.ToString()),
83 new KeyValuePair<string, object?>("clientType", TokenInformation.ClientType.ToString()));
84
85 return true;
86 }
87
88 private static async Task<bool> ForceTokenReportAsync(TokenInformation TokenInformation)
89 {
90 string OldToken = await RuntimeSettings.GetAsync(Constants.Settings.PushNotificationToken, string.Empty);
91 DateTime ReportDate = await RuntimeSettings.GetAsync(Constants.Settings.PushNotificationReportDate, DateTime.MinValue);
92
93 return (DateTime.UtcNow.Subtract(ReportDate).TotalDays > 7) || (TokenInformation.Token != OldToken);
94 }
95 }
96}
Runtime setting key names.
Definition: Constants.cs:1024
const string PushNotificationReportDate
When push-notification token was reported.
Definition: Constants.cs:1038
const string PushNotificationToken
Push-notification token.
Definition: Constants.cs:1043
A set of never changing property constants and helpful values.
Definition: Constants.cs:24
Handles deduplication and reporting of push notification tokens to the broker.
async Task< bool > ReportTokenAsync(TokenInformation TokenInformation, bool ForceReport, CancellationToken CancellationToken)
Reports a push token to the broker if needed, applying deduplication and force rules.
PushTokenRegistrar(IXmppService XmppService)
Initializes a new instance of the PushTokenRegistrar class.
Contains information about a push notification token.
PushMessagingService Service
Service issuing the token
Executes async operations through a pipeline of policies (timeout, retry, bulkhead,...
Definition: PolicyRunner.cs:14
Base class that references services in the app.
Definition: ServiceRef.cs:43
static ILogService LogService
Log service.
Definition: ServiceRef.cs:214
Static class managing persistent settings.
static async Task< string > GetAsync(string Key, string DefaultValue)
Gets a string-valued setting.
static async Task< bool > SetAsync(string Key, string Value)
Sets a string-valued setting.
Handles persistence and reporting of push notification tokens.
Represents an abstraction of a connection to an XMPP Server.
Definition: IXmppService.cs:45
Definition: ImplTypes.g.cs:58