Skip to content

Commit 57892f7

Browse files
committed
Calling back from android to flutter to run blocks
1 parent 11f11a0 commit 57892f7

File tree

5 files changed

+58
-61
lines changed

5 files changed

+58
-61
lines changed

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,14 @@ public class OneSignalInAppMessages extends FlutterRegistrarResponder implements
2323
private IInAppMessageClickResult inAppMessageClickedResult;
2424
private boolean hasSetInAppMessageClickedHandler = false;
2525

26-
private static final OneSignalInAppMessages sharedInstance = new OneSignalInAppMessages();
26+
2727

2828
static void registerWith(BinaryMessenger messenger) {
29-
29+
OneSignalInAppMessages sharedInstance = new OneSignalInAppMessages();
30+
3031
sharedInstance.messenger = messenger;
3132
sharedInstance.channel = new MethodChannel(messenger, "OneSignal#inappmessages");
32-
sharedInstance.channel.setMethodCallHandler(sharedInstance);
33-
34-
35-
OneSignalInAppMessages.sharedInstance.setInAppMessageLifecycleHandler();
36-
OneSignal.getInAppMessages().setInAppMessageClickHandler(null);
33+
sharedInstance.channel.setMethodCallHandler(sharedInstance);
3734
}
3835

3936
@Override
@@ -52,6 +49,8 @@ else if (call.method.contentEquals("OneSignal#paused"))
5249
this.paused(call, result);
5350
else if (call.method.contentEquals("OneSignal#initInAppMessageClickedHandlerParams"))
5451
this.initInAppMessageClickedHandlerParams();
52+
else if (call.method.contentEquals("OneSignal#lifecycleInit"))
53+
this.lifecycleInit();
5554
else
5655
replyNotImplemented(result);
5756
}
@@ -96,36 +95,41 @@ private void initInAppMessageClickedHandlerParams() {
9695
}
9796
}
9897

98+
public void lifecycleInit() {
99+
this.setInAppMessageLifecycleHandler();
100+
OneSignal.getInAppMessages().setInAppMessageClickHandler(this);
101+
}
102+
99103
public void inAppMessageClicked(IInAppMessageClickResult action) {
100-
if (!OneSignalInAppMessages.sharedInstance.hasSetInAppMessageClickedHandler) {
101-
OneSignalInAppMessages.sharedInstance.inAppMessageClickedResult = action;
104+
if (!this.hasSetInAppMessageClickedHandler) {
105+
this.inAppMessageClickedResult = action;
102106
return;
103107
}
104108

105-
invokeMethodOnUiThread("OneSignal#handleClickedInAppMessage", OneSignalSerializer.convertInAppMessageClickedActionToMap(action));
109+
channel.invokeMethod("OneSignal#handleClickedInAppMessage", OneSignalSerializer.convertInAppMessageClickedActionToMap(action));
106110
}
107111

108112
/* in app message lifecycle */
109113
public void setInAppMessageLifecycleHandler() {
110114
OneSignal.getInAppMessages().setInAppMessageLifecycleHandler(new IInAppMessageLifecycleHandler() {
111115
@Override
112116
public void onWillDisplayInAppMessage(IInAppMessage message) {
113-
invokeMethodOnUiThread("OneSignal#onWillDisplayInAppMessage", OneSignalSerializer.convertInAppMessageToMap(message));
117+
channel.invokeMethod("OneSignal#onWillDisplayInAppMessage", OneSignalSerializer.convertInAppMessageToMap(message));
114118
}
115119

116120
@Override
117121
public void onDidDisplayInAppMessage(IInAppMessage message) {
118-
invokeMethodOnUiThread("OneSignal#onDidDisplayInAppMessage", OneSignalSerializer.convertInAppMessageToMap(message));
122+
channel.invokeMethod("OneSignal#onDidDisplayInAppMessage", OneSignalSerializer.convertInAppMessageToMap(message));
119123
}
120124

121125
@Override
122126
public void onWillDismissInAppMessage(IInAppMessage message) {
123-
invokeMethodOnUiThread("OneSignal#onWillDismissInAppMessage", OneSignalSerializer.convertInAppMessageToMap(message));
127+
channel.invokeMethod("OneSignal#onWillDismissInAppMessage", OneSignalSerializer.convertInAppMessageToMap(message));
124128
}
125129

126130
@Override
127131
public void onDidDismissInAppMessage(IInAppMessage message) {
128-
invokeMethodOnUiThread("OneSignal#onDidDismissInAppMessage", OneSignalSerializer.convertInAppMessageToMap(message));
132+
channel.invokeMethod("OneSignal#onDidDismissInAppMessage", OneSignalSerializer.convertInAppMessageToMap(message));
129133
}
130134
});
131135
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public OneSignalPlugin() {
3636
}
3737

3838
private void init(Context context, BinaryMessenger messenger)
39-
{
39+
{
4040
this.context = context;
4141
this.messenger = messenger;
4242

@@ -119,7 +119,6 @@ public void onMethodCall(MethodCall call, Result result) {
119119
private void initWithContext(MethodCall call, Result reply) {
120120
String appId = call.argument("appId");
121121
OneSignal.initWithContext(context, appId);
122-
123122
// if (hasSetRequiresPrivacyConsent && !OneSignal.userProvidedPrivacyConsent())
124123
// this.waitingForUserPrivacyConsent = true;
125124
// else

example/lib/main.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,14 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
302302
}
303303

304304
oneSignalOutcomeExamples() async {
305-
// OneSignal.Session.addOutcome("normal_1");
306-
// OneSignal.Session.addOutcome("normal_2");
305+
OneSignal.Session.addOutcome("normal_1");
306+
OneSignal.Session.addOutcome("normal_2");
307307

308-
// OneSignal.Session.addUniqueOutcome("unique_1");
309-
// OneSignal.Session.addUniqueOutcome("unique_2");
308+
OneSignal.Session.addUniqueOutcome("unique_1");
309+
OneSignal.Session.addUniqueOutcome("unique_2");
310310

311-
// OneSignal.Session.addOutcomeWithValue("value_1", 3.2);
312-
// OneSignal.Session.addOutcomeWithValue("value_2", 3.9);
311+
OneSignal.Session.addOutcomeWithValue("value_1", 3.2);
312+
OneSignal.Session.addOutcomeWithValue("value_2", 3.9);
313313
}
314314

315315
void _handleOptIn() {

lib/onesignal_flutter.dart

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,53 +30,49 @@ class OneSignal {
3030
static OneSignalSession Session = new OneSignalSession();
3131
static OneSignalLocation Location = new OneSignalLocation();
3232
static OneSignalInAppMessages InAppMessages = new OneSignalInAppMessages();
33-
3433

3534
// private channels used to bridge to ObjC/Java
3635
MethodChannel _channel = const MethodChannel('OneSignal');
37-
38-
/// The initializer for OneSignal.
36+
37+
/// The initializer for OneSignal.
3938
///
40-
/// The initializer accepts an [appId] which the developer can get
39+
/// The initializer accepts an [appId] which the developer can get
4140
/// from the OneSignal consoleas well as a dictonary of [launchOptions]
4241
void initialize(String appId) {
43-
_channel.invokeMethod(
44-
'OneSignal#initialize', {'appId': appId});
42+
_channel.invokeMethod('OneSignal#initialize', {'appId': appId});
43+
InAppMessages.lifecycleInit();
4544
}
4645

47-
/// Login to OneSignal under the user identified by the [externalId] provided.
48-
///
49-
/// The act of logging a user into the OneSignal SDK will switch the
46+
/// Login to OneSignal under the user identified by the [externalId] provided.
47+
///
48+
/// The act of logging a user into the OneSignal SDK will switch the
5049
/// [user] context to that specific user.
5150
void login(String externalId) {
52-
_channel.invokeMethod(
53-
'OneSignal#login', {'externalId': externalId});
51+
_channel.invokeMethod('OneSignal#login', {'externalId': externalId});
5452
}
5553

56-
/// Logout the user previously logged in via [login]. The [user] property now
54+
/// Logout the user previously logged in via [login]. The [user] property now
5755
///
58-
/// references a new device-scoped user. A device-scoped user has no user identity
59-
/// that can later be retrieved, except through this device as long as the app
56+
/// references a new device-scoped user. A device-scoped user has no user identity
57+
/// that can later be retrieved, except through this device as long as the app
6058
/// remains installed and the app data is not cleared.
6159
void logout() {
62-
_channel.invokeMethod(
63-
'OneSignal#logout');
60+
_channel.invokeMethod('OneSignal#logout');
6461
}
6562

66-
/// Indicates whether privacy consent has been granted.
63+
/// Indicates whether privacy consent has been granted.
6764
///
68-
/// This field is only relevant when the application has
65+
/// This field is only relevant when the application has
6966
/// opted into data privacy protections. See [requiresPrivacyConsent].
7067
Future<bool> getPrivacyConsent() async {
71-
var val =
72-
await _channel.invokeMethod("OneSignal#getPrivacyConsent");
68+
var val = await _channel.invokeMethod("OneSignal#getPrivacyConsent");
7369

7470
return val as bool;
7571
}
7672

7773
/// Sets the whether or not privacy consent has been [granted]
7874
///
79-
/// This field is only relevant when the application has
75+
/// This field is only relevant when the application has
8076
/// opted into data privacy protections. See [requiresPrivacyConsent].
8177
Future<void> setPrivacyConsent(bool granted) async {
8278
await _channel
@@ -87,8 +83,7 @@ class OneSignal {
8783
/// user's consent before it can initialize (if you set the app to
8884
/// require the user's consent)
8985
Future<bool> requiresPrivacyConsent() async {
90-
var val =
91-
await _channel.invokeMethod("OneSignal#requiresPrivacyConsent");
86+
var val = await _channel.invokeMethod("OneSignal#requiresPrivacyConsent");
9287

9388
return val as bool;
9489
}
@@ -102,8 +97,8 @@ class OneSignal {
10297
}
10398

10499
/// This method can be used to set if launch URLs should be opened in safari or
105-
/// within the application. Set to true to launch all notifications with a URL
106-
/// in the app instead of the default web browser. Make sure to call setLaunchURLsInApp
100+
/// within the application. Set to true to launch all notifications with a URL
101+
/// in the app instead of the default web browser. Make sure to call setLaunchURLsInApp
107102
/// before the initialize call.
108103
void setLaunchURLsInApp(bool launchUrlsInApp) {
109104
_channel.invokeMethod(
@@ -114,17 +109,17 @@ class OneSignal {
114109
/// Associates a temporary push token with an Activity ID on the OneSignal server.
115110
Future<void> enterLiveActivity(String activityId, String token) async {
116111
if (Platform.isIOS) {
117-
await _channel.invokeMethod("OneSignal#enterLiveActivity", {'activityId': activityId, 'token': token});
118-
}
112+
await _channel.invokeMethod("OneSignal#enterLiveActivity",
113+
{'activityId': activityId, 'token': token});
114+
}
119115
}
120116

121117
/// Only applies to iOS
122118
/// Deletes activityId associated temporary push token on the OneSignal server.
123119
Future<void> exitLiveActivity(String activityId) async {
124120
if (Platform.isIOS) {
125-
await _channel.invokeMethod("OneSignal#exitLiveActivity",
126-
{'activityId': activityId});
121+
await _channel.invokeMethod(
122+
"OneSignal#exitLiveActivity", {'activityId': activityId});
127123
}
128124
}
129-
130125
}

lib/src/inappmessages.dart

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,24 @@ typedef void OnWillDismissInAppMessageHandler(OSInAppMessage message);
1111
typedef void OnDidDismissInAppMessageHandler(OSInAppMessage message);
1212

1313
class OneSignalInAppMessages {
14-
1514
// private channels used to bridge to ObjC/Java
1615
MethodChannel _channel = const MethodChannel('OneSignal#inappmessages');
1716

18-
1917
// constructor method
2018
OneSignalInAppMessages() {
2119
this._channel.setMethodCallHandler(_handleMethod);
2220
}
2321

24-
2522
InAppMessageClickedHandler? _onInAppMessageClickedHandler;
2623
OnWillDisplayInAppMessageHandler? _onWillDisplayInAppMessageHandler;
2724
OnDidDisplayInAppMessageHandler? _onDidDisplayInAppMessageHandler;
2825
OnWillDismissInAppMessageHandler? _onWillDismissInAppMessageHandler;
2926
OnDidDismissInAppMessageHandler? _onDidDismissInAppMessageHandler;
3027

31-
/// Adds a single key, value trigger, which will trigger an in app message
28+
/// Adds a single key, value trigger, which will trigger an in app message
3229
/// if one exists matching the specific trigger added
3330
Future<void> addTrigger(String key, Object value) async {
34-
return await _channel.invokeMethod("OneSignal#addTrigger", {key : value});
31+
return await _channel.invokeMethod("OneSignal#addTrigger", {key: value});
3532
}
3633

3734
/// Adds one or more key, value triggers, which will trigger in app messages
@@ -62,11 +59,14 @@ class OneSignalInAppMessages {
6259
return await _channel.invokeMethod("OneSignal#paused", pause);
6360
}
6461

65-
/// Gets whether of not in app messages are paused
62+
/// Gets whether of not in app messages are paused
6663
Future<bool> arePaused() async {
6764
return await _channel.invokeMethod("OneSignal#arePaused");
6865
}
69-
66+
67+
Future<bool> lifecycleInit() async {
68+
return await _channel.invokeMethod("OneSignal#lifecycleInit");
69+
}
7070

7171
// Private function that gets called by ObjC/Java
7272
Future<Null> _handleMethod(MethodCall call) async {
@@ -90,7 +90,7 @@ class OneSignalInAppMessages {
9090
this._onDidDismissInAppMessageHandler != null) {
9191
this._onDidDismissInAppMessageHandler!(
9292
OSInAppMessage(call.arguments.cast<String, dynamic>()));
93-
}
93+
}
9494
return null;
9595
}
9696

@@ -128,5 +128,4 @@ class OneSignalInAppMessages {
128128
OnDidDismissInAppMessageHandler handler) {
129129
_onDidDismissInAppMessageHandler = handler;
130130
}
131-
132131
}

0 commit comments

Comments
 (0)