Skip to content

Commit 32c9dcf

Browse files
committed
Delay Notification Opened Check in Objective-C Plugin
• Delays the check to see if a notification was opened after init. This is intended to prevent situations where the developer synchronously adds the NotificationOpened handler *after* calling OneSignal.init(). • Since the SDK is checking to see if a notification was opened on cold start AND synchronously calls the handler during the init() call, this would result in apps that add the observers after calling init() not getting cold-start notification opened events. • Switches to passing in false as the default autoPrompt setting on the first call to init() (which happens before the Flutter SDK initializes and the developer calls init() with their real app ID). • Implements the kOSSettingsKeyInOmitNoAppIdLogging setting. Since the SDK calls init() twice, the first time it passes in nil as the app ID (this happens before the Flutter channel is set up). To prevent the SDK from logging a "no appID" error, we pass in the kOSSettingsKeyInOmitNoAppIdLogging setting
1 parent 593cbb3 commit 32c9dcf

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

ios/Classes/OneSignalPlugin.m

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

74+
// Wrapper SDK's call init with no app ID early on in the
75+
// app lifecycle. The developer will call init() later on
76+
// from the Flutter plugin channel.
7477
[OneSignal initWithLaunchOptions:nil appId:nil handleNotificationAction:^(OSNotificationOpenedResult *result) {
7578
@synchronized (OneSignalPlugin.sharedInstance.coldStartOpenResult) {
7679
OneSignalPlugin.sharedInstance.coldStartOpenResult = result;
7780
}
78-
}];
81+
} settings:@{kOSSettingsKeyAutoPrompt : @false, @"kOSSettingsKeyInOmitNoAppIdLogging" : @true}];
7982

8083
OneSignalPlugin.sharedInstance.channel = [FlutterMethodChannel
8184
methodChannelWithName:@"OneSignal"
@@ -141,11 +144,18 @@ - (void)initOneSignal:(FlutterMethodCall *)call withResult:(FlutterResult)result
141144
[self addObservers];
142145
}
143146

144-
@synchronized(self.coldStartOpenResult) {
145-
if (self.coldStartOpenResult) {
146-
[self handleNotificationOpened:self.coldStartOpenResult];
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+
}
147157
}
148-
}
158+
});
149159

150160
result(@[]);
151161
}

0 commit comments

Comments
 (0)