26
26
*/
27
27
28
28
using UnityEngine ;
29
+ using System ;
29
30
using System . Threading . Tasks ;
31
+ using System . Collections . Generic ;
30
32
using OneSignalSDK . Notifications ;
31
33
using OneSignalSDK . Notifications . Models ;
32
34
using OneSignalSDK . Notifications . Internal ;
33
35
using OneSignalSDK . Android . Utilities ;
34
- using System . Collections . Generic ;
36
+ using OneSignalSDK . Android . Notifications . Models ;
35
37
36
38
namespace OneSignalSDK . Android . Notifications {
37
39
internal sealed class AndroidNotificationsManager : INotificationsManager {
38
40
private readonly AndroidJavaObject _notifications ;
39
-
41
+
40
42
public AndroidNotificationsManager ( AndroidJavaClass sdkClass ) {
41
43
_notifications = sdkClass . CallStatic < AndroidJavaObject > ( "getNotifications" ) ;
42
44
}
43
45
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 ;
47
49
48
50
public bool Permission {
49
51
get => _notifications . Call < bool > ( "getPermission" ) ;
@@ -60,69 +62,90 @@ public void ClearAllNotifications() {
60
62
}
61
63
62
64
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 ) ) ;
66
68
}
67
69
68
- private sealed class InternalPermissionChangedHandler : OneSignalAwaitableAndroidJavaProxy < bool > {
70
+ private sealed class InternalPermissionObserver : OneSignalAwaitableAndroidJavaProxy < bool > {
69
71
private AndroidNotificationsManager _parent ;
70
72
71
- public InternalPermissionChangedHandler ( AndroidNotificationsManager notificationsManager ) : base ( "notifications.IPermissionChangedHandler " ) {
73
+ public InternalPermissionObserver ( AndroidNotificationsManager notificationsManager ) : base ( "notifications.IPermissionObserver " ) {
72
74
_parent = notificationsManager ;
73
75
}
74
76
75
77
/// <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
+ }
78
84
}
79
85
}
80
86
81
- private sealed class InternalNotificationWillShowInForegroundHandler : OneSignalAndroidJavaProxy {
87
+ private sealed class InternalNotificationLifecycleListener : OneSignalAndroidJavaProxy {
82
88
private AndroidNotificationsManager _parent ;
83
89
84
- public InternalNotificationWillShowInForegroundHandler ( AndroidNotificationsManager notificationsManager ) : base ( "notifications.INotificationWillShowInForegroundHandler " ) {
90
+ public InternalNotificationLifecycleListener ( AndroidNotificationsManager notificationsManager ) : base ( "notifications.INotificationLifecycleListener " ) {
85
91
_parent = notificationsManager ;
86
92
}
87
93
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 ) ;
91
100
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 ) ) ;
95
108
}
109
+ }
110
+ }
96
111
97
- var notification = _getNotification ( notifJO ) ;
112
+ public class InternalNotificationWillDisplayEventArgs : NotificationWillDisplayEventArgs {
113
+ private AndroidJavaObject _willDisplayEvent ;
98
114
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 ;
100
117
}
118
+
119
+ public override void PreventDefault ( ) => _willDisplayEvent . Call ( "preventDefault" ) ;
101
120
}
102
121
103
- private sealed class InternalNotificationClickHandler : OneSignalAndroidJavaProxy {
122
+ private sealed class InternalNotificationClickListener : OneSignalAndroidJavaProxy {
104
123
private AndroidNotificationsManager _parent ;
105
124
106
- public InternalNotificationClickHandler ( AndroidNotificationsManager notificationsManager ) : base ( "notifications.INotificationClickHandler " ) {
125
+ public InternalNotificationClickListener ( AndroidNotificationsManager notificationsManager ) : base ( "notifications.INotificationClickListener " ) {
107
126
_parent = notificationsManager ;
108
127
}
109
128
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" ) ;
113
132
var notification = _getNotification ( notifJO ) ;
114
133
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 > ( ) ;
117
136
118
- var notifClickResult = new NotificationClickedResult ( notification , action ) ;
137
+ NotificationClickEventArgs args = new NotificationClickEventArgs ( notification , result ) ;
119
138
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
+ }
121
144
}
122
145
}
123
146
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 > ( ) ;
126
149
127
150
var dataJson = notifJO . Call < AndroidJavaObject > ( "getAdditionalData" ) ;
128
151
if ( dataJson != null ) {
@@ -142,14 +165,10 @@ private static Notification _getNotification(AndroidJavaObject notifJO) {
142
165
notification . rawPayload = rawPayloadJsonStr ;
143
166
}
144
167
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 ;
151
170
152
- return action ;
171
+ return notification ;
153
172
}
154
173
}
155
174
}
0 commit comments