Skip to content

Commit a65cc3f

Browse files
committed
cleaning up otification result and action types
1 parent e5bb068 commit a65cc3f

File tree

6 files changed

+75
-56
lines changed

6 files changed

+75
-56
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
abstract class FlutterRegistrarResponder {
1616
Context context;
17-
MethodChannel channel;
17+
protected MethodChannel channel;
1818
BinaryMessenger messenger;
1919

2020
/**
@@ -69,7 +69,7 @@ private void runOnMainThread(final Runnable runnable) {
6969
}
7070

7171
void invokeMethodOnUiThread(final String methodName, final HashMap map) {
72-
final MethodChannel channel = this.channel;
72+
//final MethodChannel channel = this.channel;
7373
runOnMainThread(new Runnable() {
7474
@Override
7575
public void run() {

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

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

3939
public class OneSignalNotifications extends FlutterRegistrarResponder implements MethodCallHandler, INotificationClickHandler, INotificationWillShowInForegroundHandler, IPermissionChangedHandler {
40-
private MethodChannel channel;
40+
// private MethodChannel channel;
4141

4242
private boolean hasSetNotificationWillShowInForegroundHandler = false;
4343
private final HashMap<String, INotificationReceivedEvent> notificationReceivedEventCache = new HashMap<>();
@@ -128,12 +128,23 @@ private void completeNotification(MethodCall call, final Result reply) {
128128
@Override
129129
public void notificationClicked(INotificationClickResult result) {
130130
try {
131-
invokeMethodOnUiThread("OneSignal#handleOpenedNotification", OneSignalSerializer.convertNotificationClickedResultToMap(result));
131+
invokeMethodOnUiThread("OneSignal#handleOpenedNotification", OneSignalSerializer.convertNotificationClickResultToMap(result));
132132
} catch (JSONException e) {
133-
e.getStackTrace();
134-
// OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR,
135-
// "Encountered an error attempting to convert OSNotificationOpenResult object to hash map: " + e.getMessage());
133+
e.getStackTrace();
134+
android.util.Log.w("TestHenryTest ", e.toString());
135+
}
136+
}
137+
138+
private JSONObject getJsonFromMap(Map<String, Object> map) throws JSONException {
139+
JSONObject jsonData = new JSONObject();
140+
for (String key : map.keySet()) {
141+
Object value = map.get(key);
142+
if (value instanceof Map<?, ?>) {
143+
value = getJsonFromMap((Map<String, Object>) value);
144+
}
145+
jsonData.put(key, value);
136146
}
147+
return jsonData;
137148
}
138149

139150
@Override
@@ -148,12 +159,9 @@ public void notificationWillShowInForeground(INotificationReceivedEvent notifica
148159

149160
try {
150161
HashMap<String, Object> receivedMap = OneSignalSerializer.convertNotificationToMap(notification);
151-
152162
invokeMethodOnUiThread("OneSignal#handleNotificationWillShowInForeground", receivedMap);
153163
} catch (JSONException e) {
154164
e.getStackTrace();
155-
// OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR,
156-
// "Encountered an error attempting to convert OSNotificationReceivedEvent object to hash map: " + e.getMessage());
157165
}
158166
}
159167

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import io.flutter.plugin.common.PluginRegistry;
2222
import io.flutter.plugin.common.PluginRegistry.Registrar;
2323

24-
public class OneSignalPushSubscription extends FlutterRegistrarResponder implements MethodCallHandler {
24+
public class OneSignalPushSubscription extends FlutterRegistrarResponder implements MethodCallHandler, ISubscriptionChangedHandler {
2525
private MethodChannel channel;
2626

2727
static void registerWith(BinaryMessenger messenger) {
@@ -59,18 +59,16 @@ private void optOut(MethodCall call, Result reply) {
5959
}
6060

6161
public void lifecycleInit() {
62-
OneSignal.getUser().getPushSubscription().addChangeHandler(new ISubscriptionChangedHandler() {
63-
@Override
64-
public void onSubscriptionChanged(ISubscription subscription) {
65-
if (!(subscription instanceof IPushSubscription)){
66-
return;
67-
}
68-
IPushSubscription pushSubscription = (IPushSubscription) subscription;
69-
invokeMethodOnUiThread("OneSignal#onSubscriptionChanged", OneSignalSerializer.convertOnSubscriptionChanged(pushSubscription));
70-
}
62+
OneSignal.getUser().getPushSubscription().addChangeHandler(this);
63+
}
7164

72-
});
65+
@Override
66+
public void onSubscriptionChanged(ISubscription subscription) {
67+
if (!(subscription instanceof IPushSubscription)){
68+
return;
69+
}
70+
IPushSubscription pushSubscription = (IPushSubscription) subscription;
71+
invokeMethodOnUiThread("OneSignal#onSubscriptionChanged", OneSignalSerializer.convertOnSubscriptionChanged(pushSubscription));
7372
}
7473

75-
7674
}

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
import com.onesignal.inAppMessages.IInAppMessageClickResult;
1010

1111
import com.onesignal.notifications.INotification;
12+
13+
import com.onesignal.notifications.INotificationAction;
1214
import com.onesignal.notifications.INotificationClickResult;
1315
import com.onesignal.notifications.INotificationReceivedEvent;
14-
import com.onesignal.notifications.INotificationReceivedEvent;
1516

1617
import org.json.JSONArray;
1718
import org.json.JSONException;
@@ -36,7 +37,6 @@ static HashMap<String, Object> convertNotificationToMap(INotification notificati
3637
hash.put("groupedNotifications", notification.getGroupedNotifications());
3738
}
3839

39-
4040
hash.put("notificationId", notification.getNotificationId());
4141
hash.put("title", notification.getTitle());
4242

@@ -76,14 +76,29 @@ static HashMap<String, Object> convertNotificationToMap(INotification notificati
7676
return hash;
7777
}
7878

79-
static HashMap<String, Object> convertNotificationClickedResultToMap(INotificationClickResult openResult) throws JSONException {
79+
static HashMap<String, Object> convertNotificationClickResultToMap(INotificationClickResult openResult) throws JSONException {
80+
HashMap<String, Object> hash = new HashMap<>();
81+
82+
hash.put("notification", convertNotificationToMap(openResult.getNotification()));
83+
hash.put("action", convertNotificationActionToMap(openResult.getAction()));
84+
85+
return hash;
86+
}
87+
88+
89+
private static HashMap<String, Object> convertNotificationActionToMap(INotificationAction action) {
8090
HashMap<String, Object> hash = new HashMap<>();
8191

82-
hash.put("actionId", openResult.getAction().getActionId());
83-
hash.put("type", openResult.getAction().getType());
84-
hash.put("title", openResult.getNotification().getTitle());
85-
hash.put("message", openResult.getNotification().getBody());
86-
hash.put("additionalData", openResult.getNotification().getAdditionalData());
92+
hash.put("id", action.getActionId());
93+
94+
switch (action.getType()) {
95+
case Opened:
96+
hash.put("type", 0);
97+
break;
98+
case ActionTaken:
99+
hash.put("type", 1);
100+
}
101+
87102
return hash;
88103
}
89104

example/lib/main.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
3232
Future<void> initPlatformState() async {
3333
if (!mounted) return;
3434

35-
OneSignal.Debug.setLogLevel(OSLogLevel.debug);
35+
OneSignal.Debug.setLogLevel(OSLogLevel.none);
3636

3737
OneSignal.Debug.setAlertLevel(OSLogLevel.none);
3838

@@ -44,7 +44,7 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
4444
// Outcome Examples
4545
//oneSignalOutcomeExamples();
4646
//OneSignal.shared.logout();
47-
OneSignal.shared.login("Henry987656778");
47+
OneSignal.shared.login("Henry11111111");
4848

4949
// OneSignal.shared.setRequiresUserPrivacyConsent(_requireConsent);
5050

@@ -59,7 +59,8 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
5959

6060
OneSignal.Notifications.setNotificationWillShowInForegroundHandler(
6161
(OSNotificationReceivedEvent event) {
62-
print('FOREGROUND HANDLER CALLED WITH: ${event}');
62+
print(
63+
'FOREGROUND HANDLER CALLED WITH1: ${event.notification.jsonRepresentation()}');
6364

6465
/// Display Notification, send null to not display
6566
event.complete(null);

lib/src/notification.dart

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import 'dart:convert';
99
/// parameters (such as whether the notification was `shown`
1010
/// to the user, whether it's `silent`, etc.)
1111
class OSNotification extends JSONStringRepresentable {
12-
1312
/// The OneSignal notification ID for this notification
1413
late String notificationId;
1514

@@ -188,8 +187,7 @@ class OSNotification extends JSONStringRepresentable {
188187
this.mutableContent = json['mutableContent'] as bool?;
189188
if (json.containsKey('category'))
190189
this.category = json['category'] as String?;
191-
if (json.containsKey('badge'))
192-
this.badge = json['badge'] as int?;
190+
if (json.containsKey('badge')) this.badge = json['badge'] as int?;
193191
if (json.containsKey('badgeIncrement'))
194192
this.badgeIncrement = json['badgeIncrement'] as int?;
195193
if (json.containsKey('subtitle'))
@@ -222,8 +220,7 @@ class OSNotification extends JSONStringRepresentable {
222220
this.fromProjectNumber = json['fromProjectNumber'] as String?;
223221
if (json.containsKey("collapseId"))
224222
this.collapseId = json['collapseId'] as String?;
225-
if (json.containsKey("priority"))
226-
this.priority = json['priority'] as int?;
223+
if (json.containsKey("priority")) this.priority = json['priority'] as int?;
227224
if (json.containsKey("androidNotificationId"))
228225
this.androidNotificationId = json['androidNotificationId'] as int?;
229226
if (json.containsKey('backgroundImageLayout')) {
@@ -232,24 +229,23 @@ class OSNotification extends JSONStringRepresentable {
232229
}
233230
if (json.containsKey('groupedNotifications')) {
234231
final dynamic jsonGroupedNotifications = json['groupedNotifications'];
235-
final jsonList = jsonDecode(jsonGroupedNotifications.toString()) as List<dynamic>;
236-
this.groupedNotifications = jsonList.map((dynamic item) =>
237-
OSNotification(item as Map<String, dynamic>)).toList();
232+
final jsonList =
233+
jsonDecode(jsonGroupedNotifications.toString()) as List<dynamic>;
234+
this.groupedNotifications = jsonList
235+
.map((dynamic item) => OSNotification(item as Map<String, dynamic>))
236+
.toList();
238237
}
239-
238+
240239
// shared parameters
241240
this.notificationId = json['notificationId'] as String;
242241

243242
if (json.containsKey('templateName'))
244243
this.templateName = json['templateName'] as String?;
245244
if (json.containsKey('templateId'))
246245
this.templateId = json['templateId'] as String?;
247-
if (json.containsKey('sound'))
248-
this.sound = json['sound'] as String?;
249-
if (json.containsKey('title'))
250-
this.title = json['title'] as String?;
251-
if (json.containsKey('body'))
252-
this.body = json['body'] as String?;
246+
if (json.containsKey('sound')) this.sound = json['sound'] as String?;
247+
if (json.containsKey('title')) this.title = json['title'] as String?;
248+
if (json.containsKey('body')) this.body = json['body'] as String?;
253249
if (json.containsKey('launchUrl'))
254250
this.launchUrl = json['launchUrl'] as String?;
255251
if (json.containsKey('additionalData'))
@@ -382,25 +378,26 @@ class OSAndroidBackgroundImageLayout extends JSONStringRepresentable {
382378
}
383379

384380
class OSNotificationReceivedEvent extends JSONStringRepresentable {
385-
386381
late OSNotification notification;
387382

388383
OSNotificationReceivedEvent(Map<String, dynamic> json) {
389384
notification = OSNotification(json);
390385
}
391386

392387
void complete(OSNotification? notification) {
393-
print('OSNotificationReceivedEvent complete with notification: $notification');
388+
print(
389+
'OSNotificationReceivedEvent complete with notification: $notification');
394390
if (notification != null) {
395-
OneSignal.Notifications.completeNotification(notification.notificationId, true);
391+
OneSignal.Notifications.completeNotification(
392+
notification.notificationId, true);
396393
} else {
397-
OneSignal.Notifications.completeNotification(this.notification.notificationId, false);
394+
OneSignal.Notifications.completeNotification(
395+
this.notification.notificationId, false);
398396
}
399397
}
400398

401399
String jsonRepresentation() {
402-
return convertToJsonString({
403-
'notification': this.notification.jsonRepresentation()
404-
});
400+
return convertToJsonString(
401+
{'notification': this.notification.jsonRepresentation()});
405402
}
406-
}
403+
}

0 commit comments

Comments
 (0)