Skip to content

Commit b5e92ff

Browse files
committed
Update notification click events
notification opened -> onClickNotification
1 parent 9b08b21 commit b5e92ff

File tree

9 files changed

+108
-97
lines changed

9 files changed

+108
-97
lines changed

android/src/main/java/com/onesignal/flutter/OneSignalNotifications.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import io.flutter.plugin.common.PluginRegistry;
3838
import io.flutter.plugin.common.PluginRegistry.Registrar;
3939

40-
public class OneSignalNotifications extends FlutterRegistrarResponder implements MethodCallHandler, INotificationClickHandler, INotificationLifecycleListener, IPermissionObserver {
40+
public class OneSignalNotifications extends FlutterRegistrarResponder implements MethodCallHandler, INotificationClickListener, INotificationLifecycleListener, IPermissionObserver {
4141
private final HashMap<String, INotificationReceivedEvent> notificationOnWillDisplayEventCache = new HashMap<>();
4242

4343
static void registerWith(BinaryMessenger messenger) {
@@ -59,8 +59,6 @@ else if (call.method.contentEquals("OneSignal#removeGroupedNotifications"))
5959
this.removeGroupedNotifications(call, result);
6060
else if (call.method.contentEquals("OneSignal#clearAll"))
6161
this.clearAll(call, result);
62-
else if (call.method.contentEquals("OneSignal#initNotificationOpenedHandlerParams"))
63-
this.initNotificationOpenedHandlerParams(call, result);
6462
else if (call.method.contentEquals("OneSignal#displayNotification"))
6563
this.displayNotification(call, result);
6664
else if (call.method.contentEquals("OneSignal#preventDefault"))
@@ -95,12 +93,6 @@ private void clearAll(MethodCall call, Result result) {
9593
OneSignal.getNotifications().clearAllNotifications();
9694
replySuccess(result, null);
9795
}
98-
99-
100-
private void initNotificationOpenedHandlerParams(MethodCall call, Result result) {
101-
OneSignal.getNotifications().setNotificationClickHandler(this);
102-
replySuccess(result, null);
103-
}
10496

10597
private void displayNotification(MethodCall call, Result result) {
10698
String notificationId = call.argument("notificationId");
@@ -125,12 +117,12 @@ private void preventDefault(MethodCall call, Result result) {
125117
}
126118

127119
@Override
128-
public void notificationClicked(INotificationClickResult result) {
120+
public void onClick(INotificationClickEvent event) {
129121
try {
130-
invokeMethodOnUiThread("OneSignal#handleOpenedNotification", OneSignalSerializer.convertNotificationClickResultToMap(result));
122+
invokeMethodOnUiThread("OneSignal#onClickNotification", OneSignalSerializer.convertNotificationClickEventToMap(event));
131123
} catch (JSONException e) {
132124
e.getStackTrace();
133-
Logging.error("Encountered an error attempting to convert INotificationClickResult object to hash map:" + e.toString(), null);
125+
Logging.error("Encountered an error attempting to convert INotificationClickEvent object to hash map:" + e.toString(), null);
134126
}
135127
}
136128

@@ -165,6 +157,7 @@ public void onNotificationPermissionChange(boolean permission) {
165157

166158
private void lifecycleInit() {
167159
OneSignal.getNotifications().addLifecycleListener(this);
160+
OneSignal.getNotification().addClickListener(this);
168161
OneSignal.getNotifications().addPermissionChangedHandler(this);
169162
}
170163
}

android/src/main/java/com/onesignal/flutter/OneSignalSerializer.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,28 +81,20 @@ static HashMap<String, Object> convertNotificationWillDisplayEventToMap(INotific
8181
return hash;
8282
}
8383

84-
static HashMap<String, Object> convertNotificationClickResultToMap(INotificationClickResult openResult) throws JSONException {
84+
private static HashMap<String, Object> convertNotificationClickResultToMap(INotificationClickResult result) throws JSONException {
8585
HashMap<String, Object> hash = new HashMap<>();
8686

87-
hash.put("notification", convertNotificationToMap(openResult.getNotification()));
88-
hash.put("action", convertNotificationActionToMap(openResult.getAction()));
87+
hash.put("action_id", result.getActionId());
88+
hash.put("url", result.getUrl());
8989

9090
return hash;
9191
}
9292

93-
94-
private static HashMap<String, Object> convertNotificationActionToMap(INotificationAction action) {
93+
static HashMap<String, Object> convertNotificationClickEventToMap(INotificationClickEvent event) {
9594
HashMap<String, Object> hash = new HashMap<>();
9695

97-
hash.put("id", action.getActionId());
98-
99-
switch (action.getType()) {
100-
case Opened:
101-
hash.put("type", 0);
102-
break;
103-
case ActionTaken:
104-
hash.put("type", 1);
105-
}
96+
hash.put("notification", convertNotificationToMap(event.getNotification()));
97+
hash.put("result", convertNotificationClickResultToMap(event.getResult()));
10698

10799
return hash;
108100
}

example/lib/main.dart

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class _MyAppState extends State<MyApp>
1717
OneSignalPermissionObserver,
1818
OneSignalInAppMessageLifecycleListener,
1919
OneSignalInAppMessageClickListener,
20-
OneSignalNotificationLifecycleListener {
20+
OneSignalNotificationLifecycleListener,
21+
OneSignalNotificationClickListener {
2122
String _debugLabelString = "";
2223
String? _emailAddress;
2324
String? _smsNumber;
@@ -55,14 +56,7 @@ class _MyAppState extends State<MyApp>
5556
OneSignal.User.pushSubscription.addObserver(this);
5657
OneSignal.Notifications.addPermissionObserver(this);
5758

58-
OneSignal.Notifications.setNotificationOpenedHandler(
59-
(OSNotificationOpenedResult result) {
60-
print('NOTIFICATION OPENED HANDLER CALLED WITH: ${result}');
61-
this.setState(() {
62-
_debugLabelString =
63-
"Opened notification: \n${result.notification.jsonRepresentation().replaceAll("\\n", "\n")}";
64-
});
65-
});
59+
OneSignal.Notifications.addClickListener(this);
6660

6761
OneSignal.Notifications.addLifecycleListener(this);
6862

@@ -118,6 +112,14 @@ class _MyAppState extends State<MyApp>
118112
print("ON DID DISMISS IN APP MESSAGE ${event.message.messageId}");
119113
}
120114

115+
void onClickNotification(OSNotificationClickEvent event) {
116+
print('NOTIFICATION CLICK LISTENER CALLED WITH EVENT: $event');
117+
this.setState(() {
118+
_debugLabelString =
119+
"Clicked notification: \n${event.notification.jsonRepresentation().replaceAll("\\n", "\n")}";
120+
});
121+
}
122+
121123
void onWillDisplayNotification(OSNotificationWillDisplayEvent event) {
122124
print(
123125
'NOTIFICATION WILL DISPLAY LISTENER CALLED WITH: ${event.notification.jsonRepresentation()}');

ios/Classes/OSFlutterCategories.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@
3939
- (NSDictionary *)toJson;
4040
@end
4141

42+
@interface OSNotificationClickEvent (Flutter)
43+
- (NSDictionary *)toJson;
44+
@end
45+
46+
@interface OSNotificationClickResult (Flutter)
47+
- (NSDictionary *)toJson;
48+
@end
49+
4250
@interface OSNotificationOpenedResult (Flutter)
4351
- (NSDictionary *)toJson;
4452
@end

ios/Classes/OSFlutterCategories.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,28 @@ - (NSDictionary *)toJson {
8484
}
8585
@end
8686

87+
@implementation OSNotificationClickEvent (Flutter)
88+
- (NSDictionary *)toJson {
89+
NSMutableDictionary *json = [NSMutableDictionary new];
90+
91+
json[@"notification"] = self.notification.toJson;
92+
json[@"result"] = self.result.toJson;
93+
94+
return json;
95+
}
96+
@end
97+
98+
@implementation OSNotificationClickResult (Flutter)
99+
- (NSDictionary *)toJson {
100+
NSMutableDictionary *json = [NSMutableDictionary new];
101+
102+
json[@"action_id"] = self.actionId;
103+
json[@"url"] = self.url;
104+
105+
return json;
106+
}
107+
@end
108+
87109
@implementation OSNotificationWillDisplayEvent (Flutter)
88110
- (NSDictionary *)toJson {
89111
NSMutableDictionary *json = [NSMutableDictionary new];

ios/Classes/OSFlutterNotifications.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#import <Flutter/Flutter.h>
3131
#import <OneSignalNotifications/OneSignalNotifications.h>
3232

33-
@interface OSFlutterNotifications : NSObject<FlutterPlugin, OSNotificationPermissionObserver, OSNotificationLifecycleListener>
33+
@interface OSFlutterNotifications : NSObject<FlutterPlugin, OSNotificationPermissionObserver, OSNotificationLifecycleListener, OSNotificationClickListener>
3434

3535
@property (strong, nonatomic) FlutterMethodChannel *channel;
3636
+ (instancetype)sharedInstance;

ios/Classes/OSFlutterNotifications.m

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
6868
[self displayNotification:call withResult:result];
6969
else if ([@"OneSignal#preventDefault" isEqualToString:call.method])
7070
[self preventDefault:call withResult:result];
71-
else if ([@"OneSignal#initNotificationOpenedHandlerParams" isEqualToString:call.method])
72-
[self initNotificationOpenedHandlerParams:call withResult:result];
7371
else if ([@"OneSignal#lifecycleInit" isEqualToString:call.method])
7472
[self lifecycleInit:call withResult:result];
7573
else
@@ -101,8 +99,9 @@ - (void)registerForProvisionalAuthorization:(FlutterMethodCall *)call withResult
10199
}
102100

103101
- (void)lifecycleInit:(FlutterMethodCall *)call withResult:(FlutterResult)result {
104-
[OneSignal.Notifications addPermissionObserver:self];
105102
[OneSignal.Notifications addLifecycleListener:self];
103+
[OneSignal.Notifications addClickListener:self];
104+
[OneSignal.Notifications addPermissionObserver:self];
106105
result(nil);
107106
}
108107

@@ -139,17 +138,10 @@ - (void)displayNotification:(FlutterMethodCall *)call withResult:(FlutterResult)
139138
[event.notification display];
140139
}
141140

142-
#pragma mark Opened Notification
143-
144-
- (void)initNotificationOpenedHandlerParams:(FlutterMethodCall *)call withResult:(FlutterResult)result {
145-
[OneSignal.Notifications setNotificationOpenedHandler:^(OSNotificationOpenedResult * _Nonnull result) {
146-
[OSFlutterNotifications.sharedInstance handleNotificationOpened:result];
147-
}];
148-
result(nil);
149-
}
141+
#pragma mark Notification Click
150142

151-
- (void)handleNotificationOpened:(OSNotificationOpenedResult *)result {
152-
[self.channel invokeMethod:@"OneSignal#handleOpenedNotification" arguments:result.toJson];
143+
- (void)onClickNotification:(OSNotificationClickEvent * _Nonnull)event {
144+
[self.channel invokeMethod:@"OneSignal#onClickNotification" arguments:event.toJson];
153145
}
154146

155147

lib/src/notification.dart

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ class OSNotification extends JSONStringRepresentable {
3939
Map<String, dynamic>? additionalData;
4040

4141
/// Any buttons you want to add to the notification.
42-
/// The notificationOpened handler will provide an
43-
/// OSNotificationAction object, which will contain
44-
/// the ID of the Action the user tapped.
4542
List<OSActionButton>? buttons;
4643

4744
/// A hashmap object representing the raw key/value
@@ -271,43 +268,19 @@ class OSNotification extends JSONStringRepresentable {
271268

272269
/// An instance of this class represents a user interaction with
273270
/// your push notification, ie. if they tap a button
274-
class OSNotificationOpenedResult {
271+
class OSNotificationClickResult extends JSONStringRepresentable {
275272
//instance properties
276-
late OSNotification notification;
277-
OSNotificationAction? action;
273+
String? actionId;
274+
String? url;
278275

279276
//constructor
280-
OSNotificationOpenedResult(Map<String, dynamic> json) {
281-
this.notification =
282-
OSNotification(json['notification'].cast<String, dynamic>());
283-
284-
if (json.containsKey('action')) {
285-
this.action =
286-
OSNotificationAction(json['action'].cast<String, dynamic>());
287-
}
277+
OSNotificationClickResult(Map<String, dynamic> json) {
278+
this.actionId = json['action_id'];
279+
this.url = json['url'];
288280
}
289-
}
290-
291-
/// Represents an action taken on a push notification, such as
292-
/// tapping the notification (or a button on the notification),
293-
/// or if your `inFocusDisplayType` is set to true - if they
294-
/// tapped 'close'.
295-
class OSNotificationAction {
296-
/// An enum that represents whether the user `opened` or
297-
/// took a more specific `action` (such as tapping a button
298-
/// on the notification)
299-
late OSNotificationActionType type;
300-
301-
/// The ID of the button on your notification
302-
/// that the user tapped
303-
String? actionId;
304281

305-
OSNotificationAction(Map<String, dynamic> json) {
306-
this.type = OSNotificationActionType.opened;
307-
this.actionId = json['id'] as String?;
308-
309-
if (json.containsKey('type'))
310-
this.type = OSNotificationActionType.values[json['type'] as int];
282+
String jsonRepresentation() {
283+
return convertToJsonString({'action_id': this.actionId, 'url': this.url});
311284
}
312285
}
313286

@@ -399,3 +372,26 @@ class OSNotificationWillDisplayEvent extends JSONStringRepresentable {
399372
{'notification': this.notification.jsonRepresentation()});
400373
}
401374
}
375+
376+
class OSNotificationClickEvent extends JSONStringRepresentable {
377+
late OSNotification notification;
378+
late OSNotificationClickResult result;
379+
380+
OSNotificationClickEvent(Map<String, dynamic> json) {
381+
notification = OSNotification(json["notification"].cast<String, dynamic>());
382+
result = OSNotificationClickResult(json["result"].cast<String, dynamic>());
383+
}
384+
385+
void preventDefault() {
386+
if (notification != null) {
387+
OneSignal.Notifications.preventDefault(notification.notificationId);
388+
}
389+
}
390+
391+
String jsonRepresentation() {
392+
return convertToJsonString({
393+
'notification': this.notification.jsonRepresentation(),
394+
'result': this.result.jsonRepresentation()
395+
});
396+
}
397+
}

lib/src/notifications.dart

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ import 'package:onesignal_flutter/src/defines.dart';
55
import 'package:onesignal_flutter/src/notification.dart';
66
import 'package:onesignal_flutter/src/permission.dart';
77

8-
typedef void OpenedNotificationHandler(OSNotificationOpenedResult openedResult);
9-
108
class OneSignalNotificationLifecycleListener {
119
void onWillDisplayNotification(OSNotificationWillDisplayEvent event) {}
1210
}
1311

12+
class OneSignalNotificationClickListener {
13+
void onClickNotification(OSNotificationClickEvent event) {}
14+
}
15+
1416
class OneSignalNotifications {
15-
// event handlers
16-
OpenedNotificationHandler? _onOpenedNotification;
17+
// event listeners
18+
List<OneSignalNotificationClickListener> _clickListeners =
19+
<OneSignalNotificationClickListener>[];
1720
List<OneSignalNotificationLifecycleListener> _lifecycleListeners =
1821
<OneSignalNotificationLifecycleListener>[];
1922

@@ -116,16 +119,15 @@ class OneSignalNotifications {
116119

117120
Future<void> lifecycleInit() async {
118121
_permission = await _channel.invokeMethod("OneSignal#permission");
119-
await _channel
120-
.invokeMethod("OneSignal#initNotificationOpenedHandlerParams");
121122
return await _channel.invokeMethod("OneSignal#lifecycleInit");
122123
}
123124

124125
Future<Null> _handleMethod(MethodCall call) async {
125-
if (call.method == 'OneSignal#handleOpenedNotification' &&
126-
this._onOpenedNotification != null) {
127-
this._onOpenedNotification!(
128-
OSNotificationOpenedResult(call.arguments.cast<String, dynamic>()));
126+
if (call.method == 'OneSignal#onClickNotification') {
127+
for (var listener in _clickListeners) {
128+
listener.onClickNotification(
129+
OSNotificationClickEvent(call.arguments.cast<String, dynamic>()));
130+
}
129131
} else if (call.method == 'OneSignal#onWillDisplayNotification') {
130132
for (var listener in _lifecycleListeners) {
131133
listener.onWillDisplayNotification(OSNotificationWillDisplayEvent(
@@ -164,10 +166,14 @@ class OneSignalNotifications {
164166
"OneSignal#displayNotification", {'notificationId': notificationId});
165167
}
166168

167-
/// The notification opened handler is called whenever the user opens a
169+
/// The notification click listener is called whenever the user opens a
168170
/// OneSignal push notification, or taps an action button on a notification.
169-
void setNotificationOpenedHandler(OpenedNotificationHandler handler) {
170-
_onOpenedNotification = handler;
171+
void addClickListener(OneSignalNotificationClickListener listener) {
172+
_clickListeners.add(listener);
173+
}
174+
175+
void removeClickListener(OneSignalNotificationClickListener listener) {
176+
_clickListeners.remove(listener);
171177
}
172178
}
173179

0 commit comments

Comments
 (0)