Skip to content

Commit 5ae831b

Browse files
committed
Fix Cold-Start Notification Opened Handler
• Previously, if a user tapped a notification while the app was closed, the Flutter OneSignal SDK wasn't correctly calling the notification opened handler • Fixed by adding a handleNotificationAction block for the first OneSignal init() call, which will save a reference to the OSNotificationOpenedResult object until the Flutter channel is up.
1 parent a184927 commit 5ae831b

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

ios/Classes/OneSignalPlugin.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#import <OneSignal/OneSignal.h>
3030

3131
@interface OneSignalPlugin : NSObject<FlutterPlugin, OSSubscriptionObserver, OSPermissionObserver, OSEmailSubscriptionObserver>
32-
@property (strong, nonatomic) FlutterMethodChannel *channel;
32+
33+
// Do NOT initialize instances of this class.
34+
// You must only reference the shared instance.
3335
+ (instancetype)sharedInstance;
3436
@end

ios/Classes/OneSignalPlugin.m

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,32 @@
3030
#import "OneSignalTagsController.h"
3131

3232
@interface OneSignalPlugin ()
33-
@property (nonatomic) BOOL waitingForUserConsent;
33+
34+
@property (strong, nonatomic) FlutterMethodChannel *channel;
35+
36+
/*
37+
Will be true if the SDK is waiting for the
38+
user's consent before initializing.
39+
This is important because if the plugin
40+
doesn't know the SDK is waiting for consent,
41+
it will add the observers (ie. subscription)
42+
*/
43+
@property (atomic) BOOL waitingForUserConsent;
44+
45+
/*
46+
holds reference to any notifications received before the
47+
flutter runtime channel has been opened
48+
Thus, if a user taps a notification while the app is
49+
terminated, the SDK will still notify the app once the
50+
channel is open
51+
*/
52+
@property (strong, nonatomic) OSNotificationOpenedResult *coldStartOpenResult;
53+
3454
@end
3555

36-
@implementation OneSignalPlugin
3756

38-
BOOL waitingForUserConsent;
57+
58+
@implementation OneSignalPlugin
3959

4060
+ (instancetype)sharedInstance
4161
{
@@ -50,7 +70,10 @@ + (instancetype)sharedInstance
5070

5171
#pragma mark FlutterPlugin
5272
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
53-
[OneSignal initWithLaunchOptions:nil appId:nil];
73+
74+
[OneSignal initWithLaunchOptions:nil appId:nil handleNotificationAction:^(OSNotificationOpenedResult *result) {
75+
76+
}];
5477

5578
OneSignalPlugin.sharedInstance.channel = [FlutterMethodChannel
5679
methodChannelWithName:@"OneSignal"
@@ -111,7 +134,7 @@ - (void)initOneSignal:(FlutterMethodCall *)call withResult:(FlutterResult)result
111134
// until consent has been provided.
112135

113136
if (OneSignal.requiresUserPrivacyConsent) {
114-
waitingForUserConsent = true;
137+
self.waitingForUserConsent = true;
115138
} else {
116139
[self addObservers];
117140
}
@@ -135,7 +158,7 @@ -(void)changeConsentStatus:(FlutterMethodCall *)call withResult:(FlutterResult)r
135158

136159
[OneSignal consentGranted:granted];
137160

138-
if (waitingForUserConsent && granted) {
161+
if (self.waitingForUserConsent && granted) {
139162
[self addObservers];
140163
}
141164

0 commit comments

Comments
 (0)