Skip to content

Commit e16e94d

Browse files
committed
Adding android impl behind methods in the notifications namespace
1 parent ed2abde commit e16e94d

File tree

6 files changed

+302
-38
lines changed

6 files changed

+302
-38
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package com.onesignal.flutter;
2+
3+
import com.onesignal.OneSignal;
4+
import com.onesignal.Continue;
5+
6+
import com.onesignal.notifications.INotification;
7+
import com.onesignal.notifications.INotificationClickHandler;
8+
import com.onesignal.notifications.INotificationClickResult;
9+
import com.onesignal.notifications.INotificationReceivedEvent;
10+
import com.onesignal.notifications.IRemoteNotificationReceivedHandler;
11+
12+
import com.onesignal.notifications.INotificationWillShowInForegroundHandler;
13+
import com.onesignal.notifications.INotificationReceivedEvent;
14+
import com.onesignal.notifications.IRemoteNotificationReceivedHandler;
15+
16+
import com.onesignal.user.subscriptions.ISubscription;
17+
import com.onesignal.user.subscriptions.IPushSubscription;
18+
import com.onesignal.notifications.IPermissionChangedHandler;
19+
20+
import org.json.JSONException;
21+
import org.json.JSONObject;
22+
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
import java.util.concurrent.atomic.AtomicBoolean;
26+
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.concurrent.atomic.AtomicBoolean;
30+
31+
import io.flutter.plugin.common.BinaryMessenger;
32+
import io.flutter.plugin.common.MethodCall;
33+
import io.flutter.plugin.common.MethodChannel;
34+
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
35+
import io.flutter.plugin.common.MethodChannel.Result;
36+
import io.flutter.plugin.common.PluginRegistry;
37+
import io.flutter.plugin.common.PluginRegistry.Registrar;
38+
39+
public class OneSignalNotifications extends FlutterRegistrarResponder implements MethodCallHandler, INotificationClickHandler, INotificationWillShowInForegroundHandler, IPermissionChangedHandler {
40+
private MethodChannel channel;
41+
42+
private boolean hasSetNotificationWillShowInForegroundHandler = false;
43+
private final HashMap<String, INotificationReceivedEvent> notificationReceivedEventCache = new HashMap<>();
44+
45+
46+
static void registerWith(BinaryMessenger messenger) {
47+
OneSignalNotifications controller = new OneSignalNotifications();
48+
controller.messenger = messenger;
49+
controller.channel = new MethodChannel(messenger, "OneSignal#notifications");
50+
controller.channel.setMethodCallHandler(controller);
51+
}
52+
53+
@Override
54+
public void onMethodCall(MethodCall call, Result result) {
55+
if (call.method.contentEquals("OneSignal#getPermission"))
56+
replySuccess(result, OneSignal.getNotifications().getPermission());
57+
else if (call.method.contentEquals("OneSignal#requestPermission"))
58+
this.requestPermission(call, result);
59+
else if (call.method.contentEquals("OneSignal#removeNotification"))
60+
this.removeNotification(call, result);
61+
else if (call.method.contentEquals("OneSignal#removeGroupedNotifications"))
62+
this.removeGroupedNotifications(call, result);
63+
else if (call.method.contentEquals("OneSignal#clearAll"))
64+
this.clearAll(call, result);
65+
else if (call.method.contentEquals("OneSignal#initNotificationOpenedHandlerParams"))
66+
this.initNotificationOpenedHandlerParams();
67+
else if (call.method.contentEquals("OneSignal#initNotificationWillShowInForegroundHandlerParams"))
68+
this.initNotificationWillShowInForegroundHandlerParams();
69+
else if (call.method.contentEquals("OneSignal#completeNotification"))
70+
this.initNotificationWillShowInForegroundHandlerParams();
71+
else if (call.method.contentEquals("OneSignal#lifecycleInit"))
72+
this.lifecycleInit();
73+
else
74+
replyNotImplemented(result);
75+
}
76+
77+
private void requestPermission(MethodCall call, Result result) {
78+
boolean fallback = call.argument("fallback");
79+
OneSignal.getNotifications().requestPermission(fallback, Continue.none());
80+
}
81+
82+
private void removeNotification(MethodCall call, Result result) {
83+
int notificationId = call.argument("notificationId");
84+
OneSignal.getNotifications().removeNotification(notificationId);
85+
86+
replySuccess(result, null);
87+
}
88+
89+
private void removeGroupedNotifications(MethodCall call, Result result) {
90+
String notificationGroup = call.argument("notificationGroup");
91+
OneSignal.getNotifications().removeGroupedNotifications(notificationGroup);
92+
93+
replySuccess(result, null);
94+
}
95+
96+
private void clearAll(MethodCall call, Result result) {
97+
OneSignal.getNotifications().clearAllNotifications();
98+
replySuccess(result, null);
99+
}
100+
101+
102+
private void initNotificationOpenedHandlerParams() {
103+
OneSignal.getNotifications().setNotificationClickHandler(this);
104+
}
105+
106+
107+
private void initNotificationWillShowInForegroundHandlerParams() {
108+
this.hasSetNotificationWillShowInForegroundHandler = true;
109+
}
110+
111+
private void completeNotification(MethodCall call, final Result reply) {
112+
String notificationId = call.argument("notificationId");
113+
boolean shouldDisplay = call.argument("shouldDisplay");
114+
INotificationReceivedEvent notificationReceivedEvent = notificationReceivedEventCache.get(notificationId);
115+
116+
if (notificationReceivedEvent == null) {
117+
//OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR, "Could not find notification completion block with id: " + notificationId);
118+
return;
119+
}
120+
121+
if (shouldDisplay) {
122+
notificationReceivedEvent.complete(notificationReceivedEvent.getNotification());
123+
} else {
124+
notificationReceivedEvent.complete(null);
125+
}
126+
}
127+
128+
@Override
129+
public void notificationClicked(INotificationClickResult result) {
130+
try {
131+
channel.invokeMethod("OneSignal#handleOpenedNotification", OneSignalSerializer.convertNotificationClickedResultToMap(result));
132+
} 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());
136+
}
137+
}
138+
139+
@Override
140+
public void notificationWillShowInForeground(INotificationReceivedEvent notificationReceivedEvent) {
141+
if (!this.hasSetNotificationWillShowInForegroundHandler) {
142+
notificationReceivedEvent.complete(notificationReceivedEvent.getNotification());
143+
return;
144+
}
145+
146+
INotification notification = notificationReceivedEvent.getNotification();
147+
notificationReceivedEventCache.put(notification.getNotificationId(), notificationReceivedEvent);
148+
149+
try {
150+
HashMap<String, Object> receivedMap = OneSignalSerializer.convertNotificationToMap(notification);
151+
channel.invokeMethod("OneSignal#handleNotificationWillShowInForeground", receivedMap);
152+
} catch (JSONException e) {
153+
e.getStackTrace();
154+
// OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR,
155+
// "Encountered an error attempting to convert OSNotificationReceivedEvent object to hash map: " + e.getMessage());
156+
}
157+
}
158+
159+
@Override
160+
public void onPermissionChanged(boolean permission) {
161+
162+
channel.invokeMethod("OneSignal#OSPermissionChanged", OneSignalSerializer.convertPermissionChanged(permission));
163+
}
164+
165+
public void lifecycleInit() {
166+
OneSignal.getNotifications().setNotificationWillShowInForegroundHandler(this);
167+
OneSignal.getNotifications().addPermissionChangedHandler(this);
168+
}
169+
170+
171+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ private void init(Context context, BinaryMessenger messenger)
5353
OneSignalInAppMessages.registerWith(messenger);
5454
OneSignalUser.registerWith(messenger);
5555
OneSignalPushSubscription.registerWith(messenger);
56+
OneSignalNotifications.registerWith(messenger);
5657
// OneSignalTagsController.registerWith(messenger);
5758
// OneSignalInAppMessagingController.registerWith(messenger);
5859
// OneSignalOutcomeEventsController.registerWith(messenger);

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
import com.onesignal.inAppMessages.IInAppMessage;
99
import com.onesignal.inAppMessages.IInAppMessageClickResult;
1010

11+
import com.onesignal.notifications.INotification;
12+
import com.onesignal.notifications.INotificationClickResult;
13+
import com.onesignal.notifications.INotificationReceivedEvent;
14+
import com.onesignal.notifications.INotificationReceivedEvent;
15+
1116
import org.json.JSONArray;
1217
import org.json.JSONException;
1318
import org.json.JSONObject;
@@ -18,6 +23,79 @@
1823
import java.util.List;
1924

2025
class OneSignalSerializer {
26+
27+
static HashMap<String, Object> convertNotificationToMap(INotification notification) throws JSONException {
28+
HashMap<String, Object> hash = new HashMap<>();
29+
30+
31+
hash.put("androidNotificationId", notification.getAndroidNotificationId());
32+
33+
if (notification.getGroupedNotifications() != null) {
34+
hash.put("groupKey", notification.getGroupKey());
35+
hash.put("groupMessage", notification.getGroupMessage());
36+
hash.put("groupedNotifications", notification.getGroupedNotifications());
37+
}
38+
39+
40+
hash.put("notificationId", notification.getNotificationId());
41+
hash.put("title", notification.getTitle());
42+
43+
if (notification.getBody() != null)
44+
hash.put("body", notification.getBody());
45+
if (notification.getSmallIcon() != null)
46+
hash.put("smallIcon", notification.getSmallIcon());
47+
if (notification.getLargeIcon() != null)
48+
hash.put("largeIcon", notification.getLargeIcon());
49+
if (notification.getBigPicture() != null)
50+
hash.put("bigPicture", notification.getBigPicture());
51+
if (notification.getSmallIconAccentColor() != null)
52+
hash.put("smallIconAccentColor", notification.getSmallIconAccentColor());
53+
if (notification.getLaunchURL() != null)
54+
hash.put("launchUrl", notification.getLaunchURL());
55+
if (notification.getSound() != null)
56+
hash.put("sound", notification.getSound());
57+
if (notification.getLedColor() != null)
58+
hash.put("ledColor", notification.getLedColor());
59+
hash.put("lockScreenVisibility", notification.getLockScreenVisibility());
60+
if (notification.getGroupKey() != null)
61+
hash.put("groupKey", notification.getGroupKey());
62+
if (notification.getGroupMessage() != null)
63+
hash.put("groupMessage", notification.getGroupMessage());
64+
if (notification.getFromProjectNumber() != null)
65+
hash.put("fromProjectNumber", notification.getFromProjectNumber());
66+
if (notification.getCollapseId() != null)
67+
hash.put("collapseId", notification.getCollapseId());
68+
hash.put("priority", notification.getPriority());
69+
if (notification.getAdditionalData() != null && notification.getAdditionalData().length() > 0)
70+
hash.put("additionalData", convertJSONObjectToHashMap(notification.getAdditionalData()));
71+
if (notification.getActionButtons() != null) {
72+
hash.put("actionButtons", notification.getActionButtons());
73+
}
74+
// TODO : rawPayload
75+
// hash.put("rawPayload", notification.getRawPayload());
76+
return hash;
77+
}
78+
79+
static HashMap<String, Object> convertNotificationClickedResultToMap(INotificationClickResult openResult) throws JSONException {
80+
HashMap<String, Object> hash = new HashMap<>();
81+
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());
87+
return hash;
88+
}
89+
90+
static HashMap<String, Object> convertPermissionChanged(boolean state) {
91+
HashMap<String, Object> permission = new HashMap<>();
92+
93+
permission.put("areNotificationsEnabled", state);
94+
95+
return permission;
96+
}
97+
98+
2199
static HashMap<String, Object> convertInAppMessageClickedActionToMap(IInAppMessageClickResult result) {
22100
HashMap<String, Object> hash = new HashMap<>();
23101

ios/Classes/OSFlutterNotifications.m

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,15 @@ - (void)removePermissionObserver:(FlutterMethodCall *)call withResult:(FlutterRe
122122
#pragma mark Received in Foreground Notification
123123

124124
- (void)initNotificationWillShowInForegroundHandlerParams {
125-
NSLog(@"hasSetNotificationWillShowInForegroundHandler1");
126125
self.hasSetNotificationWillShowInForegroundHandler = YES;
127126
}
128127

129128
- (void)handleNotificationWillShowInForeground:(OSNotification *)notification completion:(OSNotificationDisplayResponse)completion {
130-
NSLog(@"Notification will show in foreground1");
129+
131130
if (!self.hasSetNotificationWillShowInForegroundHandler) {
132131
completion(notification);
133132
return;
134133
}
135-
NSLog(@"Notification will show in foreground2");
136134
self.receivedNotificationCache[notification.notificationId] = notification;
137135
self.notificationCompletionCache[notification.notificationId] = completion;
138136
[self.channel invokeMethod:@"OneSignal#handleNotificationWillShowInForeground" arguments:notification.toJson];
@@ -169,7 +167,6 @@ - (void)initNotificationOpenedHandlerParams {
169167
}
170168

171169
- (void)handleNotificationOpened:(OSNotificationOpenedResult *)result {
172-
173170
[self.channel invokeMethod:@"OneSignal#handleOpenedNotification" arguments:result.toJson];
174171
}
175172

lib/onesignal_flutter.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class OneSignal {
4242
_channel.invokeMethod('OneSignal#initialize', {'appId': appId});
4343
InAppMessages.lifecycleInit();
4444
User.pushSubscription.lifecycleInit();
45+
Notifications.lifecycleInit();
4546
}
4647

4748
/// Login to OneSignal under the user identified by the [externalId] provided.

0 commit comments

Comments
 (0)