Skip to content

Commit d39635b

Browse files
committed
Change Cold Start Notification Opened Logic
• If the user opens an app by tapping a notification, the native SDK would call the NotificationOpened handler before the Flutter channel has been configured • On both the native Objective-C and Java plugins, added a property to hold the coldstart notification until the app adds the NotificationOpened handler • Sets SDK type to 'flutter' in ObjC
1 parent c6d7b0d commit d39635b

File tree

5 files changed

+56
-22
lines changed

5 files changed

+56
-22
lines changed

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class OneSignalPlugin implements MethodCallHandler, NotificationReceivedH
3737
private MethodChannel channel;
3838
private boolean didSetRequiresPrivacyConsent = false;
3939
private boolean waitingForUserPrivacyConsent = false;
40+
private OSNotificationOpenResult coldStartNotificationResult;
41+
private boolean setNotificationOpenedHandler = false;
4042

4143
public static void registerWith(Registrar registrar) {
4244

@@ -87,6 +89,10 @@ public void onMethodCall(MethodCall call, Result result) {
8789
this.setEmail(call, result);
8890
} else if (call.method.contentEquals("OneSignal#logoutEmail")) {
8991
this.logoutEmail(call, result);
92+
} else if (call.method.contentEquals("OneSignal#promptPermission")) {
93+
Log.e("onesignal", "promptPermission() is not applicable in Android.");
94+
} else if (call.method.contentEquals("OneSignal#didSetNotificationOpenedHandler")) {
95+
this.didSetNotificationOpenedHandler();
9096
} else {
9197
result.notImplemented();
9298
}
@@ -111,7 +117,6 @@ public void initOneSignal(MethodCall call, Result result) {
111117
}
112118

113119
public void addObservers() {
114-
Log.d("onesignal", "adding observers");
115120
OneSignal.addSubscriptionObserver(this);
116121
OneSignal.addEmailSubscriptionObserver(this);
117122
OneSignal.addPermissionObserver(this);
@@ -131,6 +136,12 @@ public void consentGranted(MethodCall call, Result result) {
131136
OneSignal.provideUserConsent((Boolean)args.get("granted"));
132137

133138
result.success(null);
139+
140+
if (this.waitingForUserPrivacyConsent) {
141+
this.waitingForUserPrivacyConsent = false;
142+
143+
this.addObservers();
144+
}
134145
}
135146

136147
public void setRequiresUserPrivacyConsent(MethodCall call, Result result) {
@@ -173,7 +184,11 @@ public void onFailure(JSONObject response) {
173184

174185
@Override
175186
public void onSuccess(JSONObject response) {
176-
reply.success(response);
187+
try {
188+
reply.success(OneSignalSerializer.convertJSONObjectToHashMap(response));
189+
} catch (JSONException exception) {
190+
Log.e("onesignal", "Encountered an error attempting to deserialize server response: " + exception.getMessage());
191+
}
177192
}
178193
});
179194
}
@@ -229,6 +244,14 @@ private int inFocusDisplayOptionToInt(OSInFocusDisplayOption option) {
229244
return 1;
230245
}
231246

247+
public void didSetNotificationOpenedHandler() {
248+
this.setNotificationOpenedHandler = true;
249+
if (this.coldStartNotificationResult != null) {
250+
this.notificationOpened(this.coldStartNotificationResult);
251+
this.coldStartNotificationResult = null;
252+
}
253+
}
254+
232255
@Override
233256
public void onOSSubscriptionChanged(OSSubscriptionStateChanges stateChanges) {
234257
Log.d("onesignal", "Subscription changed calling observer");
@@ -256,6 +279,12 @@ public void notificationReceived(OSNotification notification) {
256279

257280
@Override
258281
public void notificationOpened(OSNotificationOpenResult result) {
282+
if (!this.setNotificationOpenedHandler) {
283+
this.coldStartNotificationResult = result;
284+
return;
285+
}
286+
287+
Log.d("onesignal", "OS Notification Opened");
259288
try {
260289
this.channel.invokeMethod("OneSignal#handleOpenedNotification", OneSignalSerializer.convertNotificationOpenResultToMap(result));
261290
} catch (JSONException exception) {

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,15 @@ static public HashMap<Object, Object> convertNotificationPayloadToMap(OSNotifica
111111

112112
ArrayList<HashMap> buttons = new ArrayList<HashMap>();
113113

114-
for (int i = 0; i < payload.actionButtons.size(); i++) {
115-
OSNotificationPayload.ActionButton button = payload.actionButtons.get(i);
116-
117-
HashMap buttonHash = new HashMap();
118-
buttonHash.put("id", button.id);
119-
buttonHash.put("text", button.text);
120-
buttonHash.put("icon", button.icon);
114+
if (payload.actionButtons != null) {
115+
for (int i = 0; i < payload.actionButtons.size(); i++) {
116+
OSNotificationPayload.ActionButton button = payload.actionButtons.get(i);
117+
118+
HashMap buttonHash = new HashMap();
119+
buttonHash.put("id", button.id);
120+
buttonHash.put("text", button.text);
121+
buttonHash.put("icon", button.icon);
122+
}
121123
}
122124

123125
if (buttons.size() > 0)

example/lib/main.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,14 @@ class _MyAppState extends State<MyApp> {
163163

164164
var playerId = status.subscriptionStatus.userId;
165165

166+
var imgUrlString = "http://cdn1-www.dogtime.com/assets/uploads/gallery/30-impossibly-cute-puppies/impossibly-cute-puppy-2.jpg";
167+
166168
var notification = OSCreateNotification(
167169
playerIds: [playerId],
168170
content: "this is a test from OneSignal's Flutter SDK",
169171
heading: "Test Notification",
172+
iosAttachments: {"id1" : imgUrlString},
173+
bigPicture: imgUrlString,
170174
buttons: [
171175
OSActionButton(text: "test1", id: "id1"),
172176
OSActionButton(text: "test2", id: "id2")

ios/Classes/OneSignalPlugin.m

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ + (instancetype)sharedInstance
7171
#pragma mark FlutterPlugin
7272
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
7373

74+
[OneSignal setMSDKType:@"flutter"];
75+
7476
// Wrapper SDK's call init with no app ID early on in the
7577
// app lifecycle. The developer will call init() later on
7678
// from the Flutter plugin channel.
@@ -122,6 +124,8 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
122124
[self setEmail:call withResult:result];
123125
} else if ([@"OneSignal#logoutEmail" isEqualToString:call.method]) {
124126
[self logoutEmail:call withResult:result];
127+
} else if ([@"OneSignal#didSetNotificationOpenedHandler" isEqualToString:call.method]) {
128+
[self didSetNotificationOpenedHandler];
125129
} else {
126130
result(FlutterMethodNotImplemented);
127131
}
@@ -144,19 +148,6 @@ - (void)initOneSignal:(FlutterMethodCall *)call withResult:(FlutterResult)result
144148
[self addObservers];
145149
}
146150

147-
//since the developer may add the NotificationOpened handler
148-
//after they call OneSignal.init(), we delay this check to
149-
//make sure the app has had enough time to reasonably add
150-
//the NotificationOpened handler.
151-
152-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
153-
@synchronized(self.coldStartOpenResult) {
154-
if (self.coldStartOpenResult) {
155-
[self handleNotificationOpened:self.coldStartOpenResult];
156-
}
157-
}
158-
});
159-
160151
result(@[]);
161152
}
162153

@@ -234,6 +225,13 @@ - (void)oneSignalLog:(FlutterMethodCall *)call withResult:(FlutterResult)result
234225
[OneSignal onesignal_Log:(ONE_S_LOG_LEVEL)[call.arguments[@"logLevel"] integerValue] message:(NSString *)call.arguments[@"message"]];
235226
}
236227

228+
- (void)didSetNotificationOpenedHandler {
229+
if (self.coldStartOpenResult) {
230+
[self handleNotificationOpened:self.coldStartOpenResult];
231+
self.coldStartOpenResult = nil;
232+
}
233+
}
234+
237235
#pragma mark Received & Opened Notification Handlers
238236
- (void)handleReceivedNotification:(OSNotification *)notification {
239237
[self.channel invokeMethod:@"OneSignal#handleReceivedNotification" arguments:notification.toJson ? notification.toJson : @[]];

lib/onesignal.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class OneSignal {
7777
/// OneSignal push notification, or taps an action button on a notification.
7878
void setNotificationOpenedHandler(OpenedNotificationHandler handler) {
7979
_onOpenedNotification = handler;
80+
_channel.invokeMethod("OneSignal#didSetNotificationOpenedHandler");
8081
}
8182

8283
/// The subscription handler will be called whenever the user's OneSignal

0 commit comments

Comments
 (0)