Skip to content

Commit b2b7ca1

Browse files
committed
Update notification delegates to event args
1 parent 32086d7 commit b2b7ca1

12 files changed

+191
-182
lines changed

com.onesignal.unity.android/Editor/OneSignalAndroidDependencies.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
<repositories>
44
<repository>https://repo.maven.apache.org/maven2</repository>
55
</repositories>
6-
<androidPackage spec="com.onesignal:OneSignal:5.0.0-beta2" />
6+
<androidPackage spec="com.onesignal:OneSignal:5.0.0-beta4" />
77
</androidPackages>
88
</dependencies>
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,40 @@
1-
/*
2-
* Modified MIT License
3-
*
4-
* Copyright 2022 OneSignal
5-
*
6-
* Permission is hereby granted, free of charge, to any person obtaining a copy
7-
* of this software and associated documentation files (the "Software"), to deal
8-
* in the Software without restriction, including without limitation the rights
9-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10-
* copies of the Software, and to permit persons to whom the Software is
11-
* furnished to do so, subject to the following conditions:
12-
*
13-
* 1. The above copyright notice and this permission notice shall be included in
14-
* all copies or substantial portions of the Software.
15-
*
16-
* 2. All copies of substantial portions of the Software may only be used in connection
17-
* with services provided by OneSignal.
18-
*
19-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25-
* THE SOFTWARE.
26-
*/
27-
28-
using System;
29-
using OneSignalSDK.Notifications.Models;
30-
31-
namespace OneSignalSDK.Notifications.Internal {
32-
/// <summary>
33-
/// See full documentation at
34-
/// https://documentation.onesignal.com/docs/sdk-notification-event-handlers#notification-opened-event
35-
/// </summary>
36-
[Serializable] public sealed class NotificationClickedResult : INotificationClickedResult {
37-
/// <summary>
38-
/// Action the user took on the notification
39-
/// </summary>
40-
public INotificationAction Action => action;
41-
42-
/// <summary>
43-
/// Notification the user received
44-
/// </summary>
45-
public INotification Notification => notification;
46-
47-
#region Native Field Handling
48-
public NotificationAction action;
49-
public Notification notification;
50-
#endregion
51-
52-
public NotificationClickedResult() {}
53-
54-
public NotificationClickedResult(Notification notification, NotificationAction action) {
55-
this.action = action;
56-
this.notification = notification;
57-
}
58-
}
59-
}
1+
/*
2+
* Modified MIT License
3+
*
4+
* Copyright 2022 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
using System;
29+
using UnityEngine;
30+
using OneSignalSDK.Notifications.Internal;
31+
using OneSignalSDK.Notifications.Models;
32+
33+
namespace OneSignalSDK.Android.Notifications.Models {
34+
public sealed class AndroidDisplayableNotification : Notification, IDisplayableNotification {
35+
public AndroidJavaObject NotifJO { get; set; }
36+
37+
public void Display()
38+
=> NotifJO?.Call("display");
39+
}
40+
}

com.onesignal.unity.core/Runtime/Notifications/Models/INotificationAction.cs.meta renamed to com.onesignal.unity.android/Runtime/AndroidDisplayableNotification.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.onesignal.unity.android/Runtime/AndroidNotificationsManager.cs

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,26 @@
2626
*/
2727

2828
using UnityEngine;
29+
using System;
2930
using System.Threading.Tasks;
31+
using System.Collections.Generic;
3032
using OneSignalSDK.Notifications;
3133
using OneSignalSDK.Notifications.Models;
3234
using OneSignalSDK.Notifications.Internal;
3335
using OneSignalSDK.Android.Utilities;
34-
using System.Collections.Generic;
36+
using OneSignalSDK.Android.Notifications.Models;
3537

3638
namespace OneSignalSDK.Android.Notifications {
3739
internal sealed class AndroidNotificationsManager : INotificationsManager {
3840
private readonly AndroidJavaObject _notifications;
39-
41+
4042
public AndroidNotificationsManager(AndroidJavaClass sdkClass) {
4143
_notifications = sdkClass.CallStatic<AndroidJavaObject>("getNotifications");
4244
}
4345

44-
public event NotificationWillShowDelegate WillShow;
45-
public event NotificationClickedDelegate Clicked;
46-
public event PermissionChangedDelegate PermissionChanged;
46+
public event EventHandler<NotificationWillDisplayEventArgs> ForegroundWillDisplay;
47+
public event EventHandler<NotificationClickEventArgs> Clicked;
48+
public event EventHandler<NotificationPermissionChangedEventArgs> PermissionChanged;
4749

4850
public bool Permission {
4951
get => _notifications.Call<bool>("getPermission");
@@ -60,69 +62,90 @@ public void ClearAllNotifications() {
6062
}
6163

6264
public void Initialize() {
63-
_notifications.Call("addPermissionChangedHandler", new InternalPermissionChangedHandler(this));
64-
_notifications.Call("setNotificationWillShowInForegroundHandler", new InternalNotificationWillShowInForegroundHandler(this));
65-
_notifications.Call("setNotificationClickHandler", new InternalNotificationClickHandler(this));
65+
_notifications.Call("addPermissionObserver", new InternalPermissionObserver(this));
66+
_notifications.Call("addForegroundLifecycleListener", new InternalNotificationLifecycleListener(this));
67+
_notifications.Call("addClickListener", new InternalNotificationClickListener(this));
6668
}
6769

68-
private sealed class InternalPermissionChangedHandler : OneSignalAwaitableAndroidJavaProxy<bool> {
70+
private sealed class InternalPermissionObserver : OneSignalAwaitableAndroidJavaProxy<bool> {
6971
private AndroidNotificationsManager _parent;
7072

71-
public InternalPermissionChangedHandler(AndroidNotificationsManager notificationsManager) : base("notifications.IPermissionChangedHandler") {
73+
public InternalPermissionObserver(AndroidNotificationsManager notificationsManager) : base("notifications.IPermissionObserver") {
7274
_parent = notificationsManager;
7375
}
7476

7577
/// <param name="permission">boolean</param>
76-
public void onPermissionChanged(bool permission) {
77-
UnityMainThreadDispatch.Post(state => _parent.PermissionChanged?.Invoke(permission));
78+
public void onNotificationPermissionChange(bool permission) {
79+
EventHandler<NotificationPermissionChangedEventArgs> handler = _parent.PermissionChanged;
80+
if (handler != null)
81+
{
82+
UnityMainThreadDispatch.Post(state => handler(this, new NotificationPermissionChangedEventArgs(permission)));
83+
}
7884
}
7985
}
8086

81-
private sealed class InternalNotificationWillShowInForegroundHandler : OneSignalAndroidJavaProxy {
87+
private sealed class InternalNotificationLifecycleListener : OneSignalAndroidJavaProxy {
8288
private AndroidNotificationsManager _parent;
8389

84-
public InternalNotificationWillShowInForegroundHandler(AndroidNotificationsManager notificationsManager) : base("notifications.INotificationWillShowInForegroundHandler") {
90+
public InternalNotificationLifecycleListener(AndroidNotificationsManager notificationsManager) : base("notifications.INotificationLifecycleListener") {
8591
_parent = notificationsManager;
8692
}
8793

88-
/// <param name="notificationReceivedEvent">INotificationReceivedEvent</param>
89-
public void notificationWillShowInForeground(AndroidJavaObject notificationReceivedEvent) {
90-
var notifJO = notificationReceivedEvent.Call<AndroidJavaObject>("getNotification");
94+
/// <param name="willDisplayEvent">INotificationWillDisplayEvent</param>
95+
public void onWillDisplay(AndroidJavaObject willDisplayEvent) {
96+
var notifJO = willDisplayEvent.Call<AndroidJavaObject>("getNotification");
97+
var notification = _getNotification(notifJO);
98+
99+
var args = new InternalNotificationWillDisplayEventArgs(willDisplayEvent, notification);
91100

92-
if (_parent.WillShow == null) {
93-
notificationReceivedEvent.Call("complete", notifJO);
94-
return;
101+
EventHandler<NotificationWillDisplayEventArgs> handler = _parent.ForegroundWillDisplay;
102+
if (handler != null)
103+
{
104+
// We use Send instead of Post because we need to *not* return to our caller until the
105+
// event handlers have returned themselves. This allows a handler to call PreventDefault()
106+
// which will get passed down to Android in InternalNotificationWillDisplayEventArgs.
107+
UnityMainThreadDispatch.Send(state => handler(_parent, args));
95108
}
109+
}
110+
}
96111

97-
var notification = _getNotification(notifJO);
112+
public class InternalNotificationWillDisplayEventArgs : NotificationWillDisplayEventArgs {
113+
private AndroidJavaObject _willDisplayEvent;
98114

99-
UnityMainThreadDispatch.Post(state => notificationReceivedEvent.Call("complete", _parent.WillShow(notification) != null ? notifJO : null));
115+
public InternalNotificationWillDisplayEventArgs(AndroidJavaObject willDisplayEvent, IDisplayableNotification notification) : base(notification) {
116+
_willDisplayEvent = willDisplayEvent;
100117
}
118+
119+
public override void PreventDefault() => _willDisplayEvent.Call("preventDefault");
101120
}
102121

103-
private sealed class InternalNotificationClickHandler : OneSignalAndroidJavaProxy {
122+
private sealed class InternalNotificationClickListener : OneSignalAndroidJavaProxy {
104123
private AndroidNotificationsManager _parent;
105124

106-
public InternalNotificationClickHandler(AndroidNotificationsManager notificationsManager) : base("notifications.INotificationClickHandler") {
125+
public InternalNotificationClickListener(AndroidNotificationsManager notificationsManager) : base("notifications.INotificationClickListener") {
107126
_parent = notificationsManager;
108127
}
109128

110-
/// <param name="result">INotificationClickResult</param>
111-
public void notificationClicked(AndroidJavaObject result) {
112-
var notifJO = result.Call<AndroidJavaObject>("getNotification");
129+
/// <param name="clickEvent">INotificationClickEvent</param>
130+
public void onClick(AndroidJavaObject clickEvent) {
131+
var notifJO = clickEvent.Call<AndroidJavaObject>("getNotification");
113132
var notification = _getNotification(notifJO);
114133

115-
var actionJO = result.Call<AndroidJavaObject>("getAction");
116-
var action = _getAction(actionJO);
134+
var resultJO = clickEvent.Call<AndroidJavaObject>("getResult");
135+
var result = resultJO.ToSerializable<NotificationClickResult>();
117136

118-
var notifClickResult = new NotificationClickedResult(notification, action);
137+
NotificationClickEventArgs args = new NotificationClickEventArgs(notification, result);
119138

120-
UnityMainThreadDispatch.Post(state => _parent.Clicked?.Invoke(notifClickResult));
139+
EventHandler<NotificationClickEventArgs> handler = _parent.Clicked;
140+
if (handler != null)
141+
{
142+
UnityMainThreadDispatch.Post(state => handler(this, args));
143+
}
121144
}
122145
}
123146

124-
private static Notification _getNotification(AndroidJavaObject notifJO) {
125-
var notification = notifJO.ToSerializable<Notification>();
147+
private static AndroidDisplayableNotification _getNotification(AndroidJavaObject notifJO) {
148+
var notification = notifJO.ToSerializable<AndroidDisplayableNotification>();
126149

127150
var dataJson = notifJO.Call<AndroidJavaObject>("getAdditionalData");
128151
if (dataJson != null) {
@@ -142,14 +165,10 @@ private static Notification _getNotification(AndroidJavaObject notifJO) {
142165
notification.rawPayload = rawPayloadJsonStr;
143166
}
144167

145-
return notification;
146-
}
147-
148-
private static NotificationAction _getAction(AndroidJavaObject actionJO) {
149-
var action = actionJO.ToSerializable<NotificationAction>();
150-
action.type = (ActionType) actionJO.Call<AndroidJavaObject>("getType").Call<int>("ordinal");
168+
// attach the Java-Object to the notifification just built.
169+
notification.NotifJO = notifJO;
151170

152-
return action;
171+
return notification;
153172
}
154173
}
155174
}

com.onesignal.unity.core/Runtime/Notifications/INotificationsManager.cs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,55 @@
2525
* THE SOFTWARE.
2626
*/
2727

28+
using System;
2829
using System.Threading.Tasks;
2930
using OneSignalSDK.Notifications.Models;
3031

3132
namespace OneSignalSDK.Notifications {
3233
/// <summary>
3334
/// When a push notification has been received and is about to be displayed
3435
/// </summary>
35-
/// <param name="notification">Details of the notification to be shown</param>
36-
/// <returns>The notification object or null if the notification should not be displayed</returns>
37-
public delegate INotification NotificationWillShowDelegate(INotification notification);
36+
public class NotificationWillDisplayEventArgs : EventArgs
37+
{
38+
public IDisplayableNotification Notification { get; }
39+
40+
public NotificationWillDisplayEventArgs(IDisplayableNotification notification) {
41+
Notification = notification;
42+
}
43+
44+
/// <summary>
45+
/// Prevents OneSignal from displaying the notification automatically.
46+
/// </summary>
47+
public virtual void PreventDefault() {
48+
49+
}
50+
}
3851

3952
/// <summary>
4053
/// When a push notification was acted on by the user.
4154
/// </summary>
42-
/// <param name="result">The Notification clicked result describing:
43-
/// 1. The notification opened
44-
/// 2. The action taken by the user.
45-
/// </param>
46-
public delegate void NotificationClickedDelegate(INotificationClickedResult result);
55+
public class NotificationClickEventArgs : EventArgs
56+
{
57+
public INotification Notification { get; }
58+
public INotificationClickResult Result { get; }
59+
60+
public NotificationClickEventArgs(INotification notification, INotificationClickResult result) {
61+
Notification = notification;
62+
Result = result;
63+
}
64+
}
4765

4866
/// <summary>
4967
/// When the user enables or disables notifications for your app from the system settings outside of your app.
5068
/// </summary>
51-
/// <param name="permission">Boolean value for authorization of push notifications</param>
52-
public delegate void PermissionChangedDelegate(bool permission);
69+
public class NotificationPermissionChangedEventArgs : EventArgs
70+
{
71+
public bool Permission { get; }
72+
73+
public NotificationPermissionChangedEventArgs(bool permission) {
74+
Permission = permission;
75+
}
76+
}
5377

5478
/// <summary>
5579
/// The entry point to the notification SDK for OneSignal.
@@ -58,17 +82,17 @@ public interface INotificationsManager {
5882
/// <summary>
5983
/// When a push notification has been received while app is in the foreground
6084
/// </summary>
61-
event NotificationWillShowDelegate WillShow;
85+
event EventHandler<NotificationWillDisplayEventArgs> ForegroundWillDisplay;
6286

6387
/// <summary>
6488
/// When a push notification has been clicked by the user
6589
/// </summary>
66-
event NotificationClickedDelegate Clicked;
90+
event EventHandler<NotificationClickEventArgs> Clicked;
6791

6892
/// <summary>
6993
/// When this device's permissions for authorization of push notifications have changed.
7094
/// </summary>
71-
event PermissionChangedDelegate PermissionChanged;
95+
event EventHandler<NotificationPermissionChangedEventArgs> PermissionChanged;
7296

7397
/// <summary>
7498
/// Current status of permissions granted by this device for push notifications

com.onesignal.unity.core/Runtime/Notifications/Internal/Notification.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ namespace OneSignalSDK.Notifications.Internal {
301301
#endregion
302302
}
303303

304-
[Serializable] public sealed class Notification : NotificationBase, INotification {
304+
[Serializable] public class Notification : NotificationBase, INotification {
305305
/// <summary>
306306
/// Gets the notification payloads a summary notification was created from
307307
/// </summary>

0 commit comments

Comments
 (0)