Skip to content

Commit 251b8f8

Browse files
committed
Registering staticInstances to make handlers function properly
1 parent 5bffbfe commit 251b8f8

File tree

8 files changed

+31
-25
lines changed

8 files changed

+31
-25
lines changed

example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
6969
});
7070
});
7171

72-
OneSignal.InAppMessages.pause(false);
72+
OneSignal.InAppMessages.paused(false);
7373

7474
OneSignal.InAppMessages
7575
.setInAppMessageClickedHandler((OSInAppMessageAction action) {

ios/Classes/OSFlutterInAppMessages.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333
@interface OSFlutterInAppMessages : NSObject<FlutterPlugin, OSInAppMessageLifecycleHandler>
3434

3535
@property (strong, nonatomic) FlutterMethodChannel *channel;
36-
@property (atomic) BOOL hasSetInAppMessageClickedHandler;
36+
+ (instancetype)sharedInstance;
3737

38+
@property (atomic) BOOL hasSetInAppMessageClickedHandler;
3839
/*
3940
Holds reference to any in app messages received before any click action
4041
occurs on the body, button or image elements of the in app message

ios/Classes/OSFlutterInAppMessages.m

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* THE SOFTWARE.
2626
*/
2727

28-
#import "OSFlutterDebug.h"
28+
#import "OSFlutterInAppMessages.h"
2929
#import <OneSignalFramework/OneSignalFramework.h>
3030
#import "OSFlutterCategories.h"
3131

@@ -42,17 +42,17 @@ + (instancetype)sharedInstance {
4242
}
4343

4444
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
45-
OSFlutterInAppMessages *instance = [OSFlutterInAppMessages new];
46-
47-
instance.channel = [FlutterMethodChannel
45+
46+
OSFlutterInAppMessages.sharedInstance.channel = [FlutterMethodChannel
4847
methodChannelWithName:@"OneSignal#inappmessages"
4948
binaryMessenger:[registrar messenger]];
5049

51-
[registrar addMethodCallDelegate:instance channel:instance.channel];
50+
[registrar addMethodCallDelegate:OSFlutterInAppMessages.sharedInstance channel:OSFlutterInAppMessages.sharedInstance.channel];
5251

53-
[OneSignal.InAppMessages setLifecycleHandler:^(OSInAppMessageAction *action) {
54-
[self handleInAppMessageClicked:action];
52+
[OneSignal.InAppMessages setClickHandler:^(OSInAppMessageAction *action) {
53+
[OSFlutterInAppMessages.sharedInstance handleInAppMessageClicked:action];
5554
}];
55+
[OneSignal.InAppMessages setLifecycleHandler:OSFlutterInAppMessages.sharedInstance];
5656
}
5757

5858
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
@@ -65,14 +65,15 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
6565
} else if ([@"OneSignal#removeTriggers" isEqualToString:call.method]) {
6666
[self removeTriggers:call withResult:result];
6767
} else if ([@"OneSignal#clearTriggers" isEqualToString:call.method]) {
68-
result([OneSignal clearTriggers:call.arguments]);
68+
[self clearTriggers:call withResult:result];
6969
} else if ([@"OneSignal#paused" isEqualToString:call.method]) {
7070
[self paused:call withResult:result];
7171
} else if ([@"OneSignal#arePaused" isEqualToString:call.method]) {
72-
result(@([OneSignal paused];))
73-
} else if ([@"OneSignal#initInAppMessageClickedHandlerParams" isEqualToString:call.method])
74-
[self initInAppMessageClickedHandlerParams];{
75-
else
72+
result(@([OneSignal.InAppMessages paused]));
73+
} else if ([@"OneSignal#initInAppMessageClickedHandlerParams" isEqualToString:call.method]) {
74+
[self initInAppMessageClickedHandlerParams];
75+
}
76+
else{
7677
result(FlutterMethodNotImplemented);
7778
}
7879
}
@@ -91,7 +92,7 @@ - (void)removeTrigger:(FlutterMethodCall *)call withResult:(FlutterResult)result
9192

9293
- (void)removeTriggers:(FlutterMethodCall *)call withResult:(FlutterResult)result {
9394
NSArray *keys = call.arguments;
94-
[OneSignal.InAppMessages removeTriggersForKeys:keys];
95+
[OneSignal.InAppMessages removeTriggers:keys];
9596
result(nil);
9697
}
9798

ios/Classes/OSFlutterNotifications.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333
@interface OSFlutterNotifications : NSObject<FlutterPlugin, OSPermissionObserver>
3434

3535
@property (strong, nonatomic) FlutterMethodChannel *channel;
36-
36+
+ (instancetype)sharedInstance;
3737
@property (atomic) BOOL hasSetNotificationWillShowInForegroundHandler;
3838
@property (strong, nonatomic) NSMutableDictionary* notificationCompletionCache;
3939
@property (strong, nonatomic) NSMutableDictionary* receivedNotificationCache;
4040

41+
4142
@end

ios/Classes/OSFlutterNotifications.m

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ + (instancetype)sharedInstance {
4545
}
4646

4747
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
48-
OSFlutterNotifications *instance = [OSFlutterNotifications new];
48+
//OSFlutterNotifications *instance = [OSFlutterNotifications new];
4949

50-
instance.channel = [FlutterMethodChannel
50+
OSFlutterNotifications.sharedInstance.channel = [FlutterMethodChannel
5151
methodChannelWithName:@"OneSignal#notifications"
5252
binaryMessenger:[registrar messenger]];
5353

54-
[registrar addMethodCallDelegate:instance channel:instance.channel];
54+
[registrar addMethodCallDelegate:OSFlutterNotifications.sharedInstance channel:OSFlutterNotifications.sharedInstance.channel];
5555

5656
[OneSignal.Notifications setNotificationWillShowInForegroundHandler:^(OSNotification *notification, OSNotificationDisplayResponse completion) {
5757
[OSFlutterNotifications.sharedInstance handleNotificationWillShowInForeground:notification completion:completion];
@@ -122,15 +122,17 @@ - (void)removePermissionObserver:(FlutterMethodCall *)call withResult:(FlutterRe
122122
#pragma mark Received in Foreground Notification
123123

124124
- (void)initNotificationWillShowInForegroundHandlerParams {
125+
NSLog(@"hasSetNotificationWillShowInForegroundHandler1");
125126
self.hasSetNotificationWillShowInForegroundHandler = YES;
126127
}
127128

128129
- (void)handleNotificationWillShowInForeground:(OSNotification *)notification completion:(OSNotificationDisplayResponse)completion {
130+
NSLog(@"Notification will show in foreground1");
129131
if (!self.hasSetNotificationWillShowInForegroundHandler) {
130132
completion(notification);
131133
return;
132134
}
133-
135+
NSLog(@"Notification will show in foreground2");
134136
self.receivedNotificationCache[notification.notificationId] = notification;
135137
self.notificationCompletionCache[notification.notificationId] = completion;
136138
[self.channel invokeMethod:@"OneSignal#handleNotificationWillShowInForeground" arguments:notification.toJson];
@@ -167,6 +169,7 @@ - (void)initNotificationOpenedHandlerParams {
167169
}
168170

169171
- (void)handleNotificationOpened:(OSNotificationOpenedResult *)result {
172+
170173
[self.channel invokeMethod:@"OneSignal#handleOpenedNotification" arguments:result.toJson];
171174
}
172175

ios/Classes/OneSignalPlugin.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#import "OSFlutterNotifications.h"
3333
#import "OSFlutterSession.h"
3434
#import "OSFlutterLocation.h"
35+
#import "OSFlutterInAppMessages.h"
3536

3637

3738
@interface OneSignalPlugin ()
@@ -64,7 +65,7 @@ + (instancetype)sharedInstance {
6465
#pragma mark FlutterPlugin
6566
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
6667

67-
// [OneSignal setMSDKType:@"flutter"];
68+
[OneSignal setMSDKType:@"flutter"];
6869

6970
OneSignalPlugin.sharedInstance.channel = [FlutterMethodChannel
7071
methodChannelWithName:@"OneSignal"
@@ -76,6 +77,7 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
7677
[OSFlutterNotifications registerWithRegistrar:registrar];
7778
[OSFlutterSession registerWithRegistrar:registrar];
7879
[OSFlutterLocation registerWithRegistrar:registrar];
80+
[OSFlutterInAppMessages registerWithRegistrar:registrar];
7981

8082
}
8183

lib/src/inappmessages.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class OneSignalInAppMessages {
5858
}
5959

6060
/// Toggles the showing of all in app messages
61-
Future<void> pause(bool pause) async {
62-
return await _channel.invokeMethod("OneSignal#pause", pause);
61+
Future<void> paused(bool pause) async {
62+
return await _channel.invokeMethod("OneSignal#paused", pause);
6363
}
6464

6565
/// Gets whether of not in app messages are paused

lib/src/notifications.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ class OneSignalNotifications {
8787
}
8888

8989
Future<void> onOSPermissionChangedHandler(OSPermissionState state) async {
90-
print("onOSPermissionChanged update in flutter");
9190
for (var observer in _observers) {
92-
print("onOSPermissionChanged fired");
9391
observer.onOSPermissionChanged(state);
9492
}
9593
}

0 commit comments

Comments
 (0)