Skip to content

Commit bea3c31

Browse files
Progress
1 parent e27dec1 commit bea3c31

File tree

10 files changed

+110
-29
lines changed

10 files changed

+110
-29
lines changed

sample/Sample/Constants.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ namespace Sample
99
{
1010
public static class Constants
1111
{
12-
public const string ApiUrl = {API_URL};
13-
public const string UserApiKey = {USER_API_KEY};
12+
public const string ApiUrl = "https://notifo.easierlife.com";
13+
14+
public const string UserApiKey = "mbgen4xlv2b9tgmxivtei8jxxa67qfa1zq0kumqpemyx";
1415
}
1516
}

sample/Sample/ViewModels/MainPageViewModel.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
using System.Diagnostics;
1010
using MvvmHelpers;
1111
using Notifo.SDK;
12-
using Notifo.SDK.NotifoMobilePush;
13-
using Notifo.SDK.PushEventProvider;
1412
using Prism.AppModel;
1513
using Prism.Commands;
1614
using Prism.Mvvm;
@@ -20,7 +18,7 @@ namespace Sample.ViewModels
2018
{
2119
public class MainPageViewModel : BindableBase, IPageLifecycleAware
2220
{
23-
public ObservableRangeCollection<NotificationDto> Notifications { get; private set; } = new ObservableRangeCollection<NotificationDto>() { };
21+
public ObservableRangeCollection<UserNotificationDto> Notifications { get; private set; } = new ObservableRangeCollection<UserNotificationDto>() { };
2422

2523
private bool isRefreshing;
2624
public bool IsRefreshing
@@ -70,7 +68,7 @@ private void RefreshEventsAsync()
7068
{
7169
try
7270
{
73-
var notifications = await notifoService.Notifications.GetNotificationsAsync();
71+
var notifications = await notifoService.Notifications.GetMyNotificationsAsync();
7472
Notifications.ReplaceRange(notifications.Items);
7573
}
7674
catch (Exception ex)

sdk/Notifo.SDK.FirebasePlugin/PluginEventsProvider.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,26 @@ internal class PluginEventsProvider : IPushEventsProvider
1919
public event EventHandler<TokenRefreshEventArgs>? OnTokenRefresh;
2020
public event EventHandler<NotificationEventArgs>? OnNotificationReceived;
2121
public event EventHandler<NotificationEventArgs>? OnNotificationOpened;
22+
public event EventHandler<NotificationErrorEventArgs> OnError;
2223

2324
public PluginEventsProvider()
2425
{
2526
CrossFirebasePushNotification.Current.OnTokenRefresh += FirebasePushNotification_OnTokenRefresh;
2627
CrossFirebasePushNotification.Current.OnNotificationReceived += FirebasePushNotification_OnNotificationReceived;
2728
CrossFirebasePushNotification.Current.OnNotificationOpened += FirebasePushNotification_OnNotificationOpened;
29+
CrossFirebasePushNotification.Current.OnNotificationError += Current_OnNotificationError;
2830
}
2931

3032
public string Token => CrossFirebasePushNotification.Current.Token;
3133

3234
private void FirebasePushNotification_OnTokenRefresh(object source, FirebasePushNotificationTokenEventArgs e)
3335
{
34-
var args = new TokenRefreshEventArgs(e.Token);
35-
OnRefreshTokenEvent(args);
36+
OnTokenRefresh?.Invoke(this, new TokenRefreshEventArgs(e.Token));
3637
}
3738

38-
protected virtual void OnRefreshTokenEvent(TokenRefreshEventArgs args)
39+
private void Current_OnNotificationError(object source, FirebasePushNotificationErrorEventArgs e)
3940
{
40-
OnTokenRefresh?.Invoke(this, args);
41+
OnError?.Invoke(this, new NotificationErrorEventArgs(e.Message, null, source));
4142
}
4243

4344
private void FirebasePushNotification_OnNotificationReceived(object source, FirebasePushNotificationDataEventArgs e)
@@ -47,14 +48,10 @@ private void FirebasePushNotification_OnNotificationReceived(object source, Fire
4748
return;
4849
}
4950

50-
var args = new NotificationEventArgs(
51-
new UserNotificationDto().FromDictionary(new Dictionary<string, object>(e.Data)));
51+
var args =
52+
new NotificationEventArgs(
53+
new UserNotificationDto().FromDictionary(new Dictionary<string, object>(e.Data)));
5254

53-
OnNotificationReceivedEvent(args);
54-
}
55-
56-
protected virtual void OnNotificationReceivedEvent(NotificationEventArgs args)
57-
{
5855
OnNotificationReceived?.Invoke(this, args);
5956
}
6057

@@ -65,14 +62,10 @@ private void FirebasePushNotification_OnNotificationOpened(object source, Fireba
6562
return;
6663
}
6764

68-
var args = new NotificationEventArgs(
69-
new UserNotificationDto().FromDictionary(new Dictionary<string, object>(e.Data)));
65+
var args =
66+
new NotificationEventArgs(
67+
new UserNotificationDto().FromDictionary(new Dictionary<string, object>(e.Data)));
7068

71-
OnNotificationOpenedEvent(args);
72-
}
73-
74-
protected virtual void OnNotificationOpenedEvent(NotificationEventArgs args)
75-
{
7669
OnNotificationOpened?.Invoke(this, args);
7770
}
7871

sdk/Notifo.SDK/INotifoMobilePush.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public partial interface INotifoMobilePush : INotifoClient
2525
/// </summary>
2626
event EventHandler<NotificationEventArgs> OnNotificationOpened;
2727

28+
/// <summary>
29+
/// Event triggered when an error happened.
30+
/// </summary>
31+
event EventHandler<NotificationErrorEventArgs> OnError;
32+
2833
/// <summary>
2934
/// Sets the api key to use.
3035
/// </summary>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// ==========================================================================
2+
// Notifo.io
3+
// ==========================================================================
4+
// Copyright (c) Sebastian Stehle
5+
// All rights reserved. Licensed under the MIT license.
6+
// ==========================================================================
7+
8+
using System;
9+
10+
namespace Notifo.SDK
11+
{
12+
/// <summary>
13+
/// Event arguments containing the error and the source.
14+
/// </summary>
15+
public class NotificationErrorEventArgs : EventArgs
16+
{
17+
/// <summary>
18+
/// Gets the notification.
19+
/// </summary>
20+
public string Error { get; private set; }
21+
22+
/// <summary>
23+
/// Gets the exception.
24+
/// </summary>
25+
public Exception? Exception { get; private set; }
26+
27+
/// <summary>
28+
/// Gets the source.
29+
/// </summary>
30+
public object? Source { get; private set; }
31+
32+
/// <summary>
33+
/// Initializes a new instance of the <see cref="NotificationErrorEventArgs"/> class with the message and source.
34+
/// </summary>
35+
/// <param name="error">The error.</param>
36+
/// <param name="exception">The exception.</param>
37+
/// <param name="source">The source.</param>
38+
public NotificationErrorEventArgs(string error, Exception? exception, object? source)
39+
{
40+
Error = error;
41+
Exception = exception;
42+
Source = source;
43+
}
44+
}
45+
}

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ internal partial class NotifoMobilePushImplementation : INotifoMobilePush
2626
/// <inheritdoc/>
2727
public event EventHandler<NotificationEventArgs> OnNotificationOpened;
2828

29+
/// <inheritdoc/>
30+
public event EventHandler<NotificationErrorEventArgs> OnError;
31+
2932
/// <inheritdoc/>
3033
public IAppsClient Apps => clientProvider.Client.Apps;
3134

@@ -91,6 +94,7 @@ public INotifoMobilePush SetPushEventsProvider(IPushEventsProvider pushEventsPro
9194
this.pushEventsProvider.OnTokenRefresh -= PushEventsProvider_OnTokenRefresh;
9295
this.pushEventsProvider.OnNotificationReceived -= PushEventsProvider_OnNotificationReceived;
9396
this.pushEventsProvider.OnNotificationOpened -= PushEventsProvider_OnNotificationOpened;
97+
this.pushEventsProvider.OnError -= PushEventsProvider_OnError;
9498
}
9599

96100
this.pushEventsProvider = pushEventsProvider;
@@ -100,16 +104,22 @@ public INotifoMobilePush SetPushEventsProvider(IPushEventsProvider pushEventsPro
100104
this.pushEventsProvider.OnTokenRefresh += PushEventsProvider_OnTokenRefresh;
101105
this.pushEventsProvider.OnNotificationReceived += PushEventsProvider_OnNotificationReceived;
102106
this.pushEventsProvider.OnNotificationOpened += PushEventsProvider_OnNotificationOpened;
107+
this.pushEventsProvider.OnError += PushEventsProvider_OnError;
103108
}
104109

105-
token = pushEventsProvider.Token;
110+
if (!string.Equals(pushEventsProvider.Token, token))
111+
{
112+
token = pushEventsProvider.Token;
113+
114+
Register();
115+
}
106116

107117
return this;
108118
}
109119

110120
public void Register()
111121
{
112-
if (token == null)
122+
if (string.IsNullOrEmpty(token))
113123
{
114124
return;
115125
}
@@ -119,7 +129,7 @@ public void Register()
119129

120130
public void Unregister()
121131
{
122-
if (token == null)
132+
if (string.IsNullOrEmpty(token))
123133
{
124134
return;
125135
}
@@ -139,11 +149,22 @@ private void PushEventsProvider_OnNotificationOpened(object sender, Notification
139149
OnNotificationOpened?.Invoke(sender, e);
140150
}
141151

152+
private void PushEventsProvider_OnError(object sender, NotificationErrorEventArgs e)
153+
{
154+
// Forward the event to the application.
155+
OnError?.Invoke(sender, e);
156+
}
157+
142158
private void PushEventsProvider_OnTokenRefresh(object sender, TokenRefreshEventArgs e)
143159
{
144160
token = e.Token;
145161

146-
_ = commandQueue.ExecuteAsync(new TokenUnregisterCommand { Token = e.Token });
162+
Register();
163+
}
164+
165+
public void RaiseError(string error, Exception? exception, object? source)
166+
{
167+
OnError?.Invoke(this, new NotificationErrorEventArgs(error, exception, source));
147168
}
148169
}
149170
}

sdk/Notifo.SDK/NotifoMobilePush/TokenRegisterCommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public async ValueTask ExecuteAsync(
5151
}
5252
catch (Exception ex)
5353
{
54+
((NotifoMobilePushImplementation)NotifoIO.Current).RaiseError(ex.Message, ex, this);
55+
5456
Log.Error(ex, Strings.TokenRefreshFailException);
5557
throw ex;
5658
}

sdk/Notifo.SDK/NotifoMobilePush/TokenUnregisterCommand.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// All rights reserved. Licensed under the MIT license.
66
// ==========================================================================
77

8+
using System;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using Notifo.SDK.CommandQueue;
@@ -18,7 +19,15 @@ internal sealed class TokenUnregisterCommand : ICommand
1819
public async ValueTask ExecuteAsync(
1920
CancellationToken ct)
2021
{
21-
await NotifoIO.Current.MobilePush.DeleteMyTokenAsync(Token, ct);
22+
try
23+
{
24+
await NotifoIO.Current.MobilePush.DeleteMyTokenAsync(Token, ct);
25+
}
26+
catch (Exception ex)
27+
{
28+
((NotifoMobilePushImplementation)NotifoIO.Current).RaiseError(ex.Message, ex, this);
29+
throw ex;
30+
}
2231
}
2332

2433
public bool Merge(ICommand other)

sdk/Notifo.SDK/NotifoMobilePush/TrackSeenCommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public async ValueTask ExecuteAsync(
3636
}
3737
catch (Exception ex)
3838
{
39+
((NotifoMobilePushImplementation)NotifoIO.Current).RaiseError(ex.Message, ex, this);
40+
3941
Log.Error(Strings.TrackingException, ex);
4042
return;
4143
}

sdk/Notifo.SDK/PushEventProvider/IPushEventsProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public interface IPushEventsProvider
2929
/// </summary>
3030
event EventHandler<NotificationEventArgs> OnNotificationOpened;
3131

32+
/// <summary>
33+
/// Event triggered when an error happened.
34+
/// </summary>
35+
event EventHandler<NotificationErrorEventArgs> OnError;
36+
3237
/// <summary>
3338
/// Push notification token.
3439
/// </summary>

0 commit comments

Comments
 (0)