Skip to content

Commit ce0c4b0

Browse files
committed
Fix iOS Notification open handler
* Add open handler listener * Add cold start support
1 parent f727589 commit ce0c4b0

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

ios/Classes/OneSignalPlugin.m

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ it will add the observers (ie. subscription)
4444
*/
4545
@property (atomic) BOOL waitingForUserConsent;
4646

47+
@property (atomic) BOOL hasSetNotificationOpenedHandler;
48+
@property (atomic) BOOL hasSetInAppMessageClickedHandler;
4749
@property (atomic) BOOL hasSetNotificationWillShowInForegroundHandler;
4850

4951
/*
@@ -76,6 +78,9 @@ + (instancetype)sharedInstance {
7678
sharedInstance.waitingForUserConsent = false;
7779
sharedInstance.receivedNotificationCache = [NSMutableDictionary new];;
7880
sharedInstance.notificationCompletionCache = [NSMutableDictionary new];;
81+
sharedInstance.hasSetNotificationOpenedHandler = false;
82+
sharedInstance.hasSetInAppMessageClickedHandler = false;
83+
sharedInstance.hasSetNotificationWillShowInForegroundHandler = false;
7984
});
8085
return sharedInstance;
8186
}
@@ -104,12 +109,12 @@ - (void)addObservers {
104109
[OneSignal addSubscriptionObserver:self];
105110
[OneSignal addPermissionObserver:self];
106111
[OneSignal addEmailSubscriptionObserver:self];
107-
[OneSignal setNotificationOpenedHandler:^(OSNotificationOpenedResult * _Nonnull result) {
108-
[OneSignalPlugin.sharedInstance handleNotificationOpened:result];
109-
}];
110112
[OneSignal setNotificationWillShowInForegroundHandler:^(OSNotification *notification, OSNotificationDisplayResponse completion) {
111113
[OneSignalPlugin.sharedInstance handleNotificationWillShowInForeground:notification completion:completion];
112114
}];
115+
[OneSignal setNotificationOpenedHandler:^(OSNotificationOpenedResult * _Nonnull result) {
116+
[OneSignalPlugin.sharedInstance handleNotificationOpened:result];
117+
}];
113118
}
114119

115120
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
@@ -300,13 +305,17 @@ - (void)removeExternalUserId:(FlutterMethodCall *)call withResult:(FlutterResult
300305
}
301306

302307
- (void)initNotificationOpenedHandlerParams {
308+
_hasSetNotificationOpenedHandler = true;
309+
303310
if (self.coldStartOpenResult) {
304311
[self handleNotificationOpened:self.coldStartOpenResult];
305312
self.coldStartOpenResult = nil;
306313
}
307314
}
308315

309316
- (void)initInAppMessageClickedHandlerParams {
317+
_hasSetInAppMessageClickedHandler = true;
318+
310319
if (self.inAppMessageClickedResult) {
311320
[self handleInAppMessageClicked:self.inAppMessageClickedResult];
312321
self.inAppMessageClickedResult = nil;
@@ -319,6 +328,11 @@ - (void)initNotificationWillShowInForegroundHandlerParams {
319328

320329
#pragma mark Opened Notification Handlers
321330
- (void)handleNotificationOpened:(OSNotificationOpenedResult *)result {
331+
if (!self.hasSetNotificationOpenedHandler) {
332+
_coldStartOpenResult = result;
333+
return;
334+
}
335+
322336
[self.channel invokeMethod:@"OneSignal#handleOpenedNotification" arguments:result.toJson];
323337
}
324338

@@ -358,6 +372,11 @@ - (void)completeNotification:(FlutterMethodCall *)call withResult:(FlutterResult
358372

359373
#pragma mark In App Message Click Handler
360374
- (void)handleInAppMessageClicked:(OSInAppMessageAction *)action {
375+
if (!self.hasSetInAppMessageClickedHandler) {
376+
_inAppMessageClickedResult = action;
377+
return;
378+
}
379+
361380
[self.channel invokeMethod:@"OneSignal#handleClickedInAppMessage" arguments:action.toJson];
362381
}
363382

0 commit comments

Comments
 (0)