Skip to content

Commit eff22a8

Browse files
Fix internal and better cancellation.
1 parent 77f82d3 commit eff22a8

17 files changed

+391
-301
lines changed

sample/Sample.Android/NotificationHandler.cs

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

8+
using System.Threading;
9+
using System.Threading.Tasks;
810
using Android.App;
911
using Android.Content;
1012
using Android.OS;
@@ -15,16 +17,18 @@ namespace Sample.Droid
1517
{
1618
public class NotificationHandler : INotificationHandler
1719
{
18-
public void OnBuildNotification(NotificationCompat.Builder notificationBuilder, UserNotificationDto notification)
20+
public Task OnBuildNotificationAsync(NotificationCompat.Builder notificationBuilder, UserNotificationDto notification,
21+
CancellationToken ct)
1922
{
2023
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
2124
{
22-
return;
25+
return Task.CompletedTask;
2326
}
2427

2528
var soundUri = Android.Net.Uri.Parse($"{ContentResolver.SchemeAndroidResource}://{Application.Context.PackageName}/raw/announcement");
2629

2730
notificationBuilder.SetSound(soundUri);
31+
return Task.CompletedTask;
2832
}
2933
}
3034
}

sample/Sample.iOS.Shared/NotificationHandler.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@
66
// ==========================================================================
77

88
using Notifo.SDK;
9+
using System.Threading;
10+
using System.Threading.Tasks;
911
using UserNotifications;
1012

1113
namespace Sample.iOS.Shared
1214
{
1315
public class NotificationHandler : INotificationHandler
1416
{
15-
public void OnBuildNotification(UNMutableNotificationContent content, UserNotificationDto notification)
17+
public Task OnBuildNotificationAsync(UNMutableNotificationContent content, UserNotificationDto notification,
18+
CancellationToken ct)
1619
{
1720
content.Sound = UNNotificationSound.GetSound("announcement.caf");
21+
22+
return Task.CompletedTask;
1823
}
1924
}
2025
}

sample/SampleNotificationServiceExtension/NotificationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public override async void DidReceiveNotificationRequest(UNNotificationRequest r
3737
.SetSharedName("group.io.notifo.xamarin.sample")
3838
.SetNotificationHandler(new NotificationHandler());
3939

40-
await NotifoIO.Current.DidReceiveNotificationRequestAsync(request, BestAttemptContent);
40+
await NotifoIO.Current.DidReceiveNotificationRequestAsync(request, BestAttemptContent);
4141

4242
// Display the notification.
4343
ContentHandler(BestAttemptContent);

sdk/Notifo.SDK.FirebasePlugin/NotifoMobilePushExtensions.ios.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ static bool ContainsPullRefreshRequest(NSDictionary data)
6262
return aps?.ContainsKey(new NSString(Constants.ContentAvailableKey)) == true;
6363
}
6464

65-
if (ContainsPullRefreshRequest(data))
65+
if (ContainsPullRefreshRequest(data) && notifo is InternalIOSPushAdapter adapter)
6666
{
67-
await notifo.DidReceivePullRefreshRequestAsync();
67+
await adapter.DidReceivePullRefreshRequestAsync();
6868
}
6969

7070
FirebasePushNotificationManager.DidReceiveMessage(data);
@@ -99,7 +99,10 @@ public static void RemoteNotificationRegistrationFailed(this INotifoMobilePush n
9999
/// <param name="completionHandler">The action to execute when you have finished processing the user's response.</param>
100100
public static void DidReceiveNotificationResponse(this INotifoMobilePush notifo, UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
101101
{
102-
notifo.DidReceiveNotificationResponse(center, response, completionHandler);
102+
if (notifo is InternalIOSPushAdapter adapter)
103+
{
104+
adapter.DidReceiveNotificationResponse(response);
105+
}
103106

104107
if (CrossFirebasePushNotification.Current is IUNUserNotificationCenterDelegate notificationDelegate)
105108
{

sdk/Notifo.SDK.FirebasePlugin/NotifoPushNotificationHandler.android.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,11 @@ namespace Notifo.SDK.FirebasePlugin;
1717

1818
internal class NotifoPushNotificationHandler : DefaultPushNotificationHandler
1919
{
20-
private NotifoMobilePushImplementation notifoMobilePush;
21-
22-
public NotifoPushNotificationHandler()
23-
{
24-
notifoMobilePush = (NotifoMobilePushImplementation)NotifoIO.Current;
25-
}
26-
2720
public override void OnReceived(IDictionary<string, object> parameters)
2821
{
2922
NotifoIO.Current.RaiseDebug(Strings.ReceivedNotification, this, parameters);
3023

31-
var notification = new UserNotificationDto()
32-
.FromDictionary(new Dictionary<string, object>(parameters));
24+
var notification = new UserNotificationDto().FromDictionary(parameters);
3325

3426
if (notification.Silent)
3527
{
@@ -48,9 +40,11 @@ public override void OnReceived(IDictionary<string, object> parameters)
4840

4941
public override void OnBuildNotification(NotificationCompat.Builder notificationBuilder, IDictionary<string, object> parameters)
5042
{
51-
var notification = new UserNotificationDto()
52-
.FromDictionary(new Dictionary<string, object>(parameters));
43+
var notification = new UserNotificationDto().FromDictionary(parameters);
5344

54-
notifoMobilePush.OnBuildNotification(notificationBuilder, notification);
45+
if (NotifoIO.Current is InternalAndroidPushAdapter adapter)
46+
{
47+
_ = adapter.OnBuildNotificationAsync(notificationBuilder, notification);
48+
}
5549
}
5650
}

sdk/Notifo.SDK/Constants.cs

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

8+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
9+
810
namespace Notifo.SDK;
911

10-
internal static class Constants
12+
public static class Constants
1113
{
1214
public const string ApsAlertBodyKey = "aps.alert.body";
1315

sdk/Notifo.SDK/Extensions/UserNotificationDtoExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
using System;
99
using System.Collections.Generic;
1010

11+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
12+
1113
namespace Notifo.SDK.Extensions;
1214

13-
internal static class UserNotificationDtoExtensions
15+
public static class UserNotificationDtoExtensions
1416
{
1517
public static Dictionary<string, string> ToDictionary(this UserNotificationDto notification)
1618
{
@@ -81,7 +83,7 @@ public static Dictionary<string, string> ToDictionary(this UserNotificationDto n
8183
return data;
8284
}
8385

84-
public static UserNotificationDto FromDictionary(this UserNotificationDto notification, Dictionary<string, object> data)
86+
public static UserNotificationDto FromDictionary(this UserNotificationDto notification, IDictionary<string, object> data)
8587
{
8688
if (data.TryGetValue(Constants.IdKey, out var id) && Guid.TryParse(id.ToString(), out var guid))
8789
{

sdk/Notifo.SDK/INotificationHandler.android.cs

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

8+
using System.Threading;
9+
using System.Threading.Tasks;
810
using AndroidX.Core.App;
911

1012
namespace Notifo.SDK;
@@ -19,5 +21,8 @@ public interface INotificationHandler
1921
/// </summary>
2022
/// <param name="notificationBuilder">The notification builder.</param>
2123
/// <param name="notification">The notification.</param>
22-
void OnBuildNotification(NotificationCompat.Builder notificationBuilder, UserNotificationDto notification);
24+
/// <param name="ct">The cancellation token to abort waiting.</param>
25+
/// <returns>The tasks.</returns>
26+
Task OnBuildNotificationAsync(NotificationCompat.Builder notificationBuilder, UserNotificationDto notification,
27+
CancellationToken ct);
2328
}

sdk/Notifo.SDK/INotificationHandler.ios.cs

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

8+
using System.Threading;
9+
using System.Threading.Tasks;
810
using UserNotifications;
911

1012
namespace Notifo.SDK;
@@ -19,5 +21,8 @@ public interface INotificationHandler
1921
/// </summary>
2022
/// <param name="content">The notification content.</param>
2123
/// <param name="notification">The notification.</param>
22-
void OnBuildNotification(UNMutableNotificationContent content, UserNotificationDto notification);
24+
/// <param name="ct">The cancellation token to abort waiting.</param>
25+
/// <returns>The tasks.</returns>
26+
Task OnBuildNotificationAsync(UNMutableNotificationContent content, UserNotificationDto notification,
27+
CancellationToken ct);
2328
}

sdk/Notifo.SDK/INotifoMobilePush.android.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// All rights reserved. Licensed under the MIT license.
66
// ==========================================================================
77

8+
using System.Threading.Tasks;
9+
using AndroidX.Core.App;
10+
811
namespace Notifo.SDK;
912

1013
/// <summary>
@@ -26,3 +29,19 @@ public partial interface INotifoMobilePush
2629
/// <returns>The current instance.</returns>
2730
INotifoMobilePush SetImageCacheCapacity(int capacity);
2831
}
32+
33+
/// <summary>
34+
/// Provides methods that should not be triggered from user code.
35+
/// </summary>
36+
#pragma warning disable IDE1006 // Naming Styles
37+
public interface InternalAndroidPushAdapter
38+
#pragma warning restore IDE1006 // Naming Styles
39+
{
40+
/// <summary>
41+
/// Called when a notification has been received.
42+
/// </summary>
43+
/// <param name="notificationBuilder">The android notification.</param>
44+
/// <param name="notification">The notifo notification.</param>
45+
/// <returns>The tasks.</returns>
46+
Task OnBuildNotificationAsync(NotificationCompat.Builder notificationBuilder, UserNotificationDto notification);
47+
}

0 commit comments

Comments
 (0)