Skip to content

Commit b644e28

Browse files
Improve registration.
1 parent af17fbc commit b644e28

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

sdk/Notifo.SDK.FirebasePlugin/PluginFirebasePushNotifications/IFirebasePushNotification.shared.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,5 @@ public interface IFirebasePushNotification
172172
void RemoveNotification(string tag, int id);
173173

174174
Task<string> GetTokenAsync();
175-
176175
}
177176
}

sdk/Notifo.SDK/INotifoMobilePush.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ public partial interface INotifoMobilePush : INotifoClient
7878
/// </summary>
7979
void Register();
8080

81+
/// <summary>
82+
/// Register for notifications on demand.
83+
/// </summary>
84+
/// <param name="tokenToRegister">The token to register.</param>
85+
void Register(string? tokenToRegister);
86+
8187
/// <summary>
8288
/// Unregister notifications on demand.
8389
/// </summary>

sdk/Notifo.SDK/NotifoMobilePush/NotifoMobilePushImplementation.shared.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,37 @@ public void RaiseError(string error, Exception? exception, object? source)
167167
/// <inheritdoc/>
168168
public void Register()
169169
{
170-
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(clientProvider.ApiKey))
170+
async Task RegisterAsync()
171+
{
172+
if (pushEventsProvider == null)
173+
{
174+
return;
175+
}
176+
177+
// Always fetch the token to avoid any consistency issues.
178+
var tokenToRegister = await pushEventsProvider.GetTokenAsync();
179+
180+
Register(tokenToRegister);
181+
}
182+
183+
_ = RegisterAsync();
184+
}
185+
186+
/// <inheritdoc/>
187+
public void Register(string? tokenToRegister)
188+
{
189+
if (string.IsNullOrEmpty(tokenToRegister))
171190
{
172191
return;
173192
}
174193

175-
commandQueue.Run(new TokenRegisterCommand { Token = token });
194+
commandQueue.Run(new TokenRegisterCommand { Token = tokenToRegister });
176195
}
177196

178197
/// <inheritdoc/>
179198
public void Unregister()
180199
{
181-
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(clientProvider.ApiKey))
200+
if (string.IsNullOrEmpty(token))
182201
{
183202
return;
184203
}
@@ -221,7 +240,12 @@ private void UpdateToken(string? newToken)
221240
{
222241
token = newToken;
223242

224-
Register();
243+
// Only register the token if the client is already configured to avoid warning logs.
244+
if (!string.IsNullOrEmpty(clientProvider.ApiKey) &&
245+
!string.IsNullOrEmpty(clientProvider.ApiUrl))
246+
{
247+
Register(newToken);
248+
}
225249
}
226250
}
227251
}

0 commit comments

Comments
 (0)