Skip to content

Commit 111e1f3

Browse files
committed
Pass callbacks to Unity Main Thread
1 parent aa490d6 commit 111e1f3

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

com.onesignal.unity.android/Runtime/AndroidInAppMessagesManager.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,20 @@ public IInAppMessageLifecycleHandler(AndroidInAppMessagesManager inAppMessagesMa
8383
}
8484

8585
/// <param name="message">IInAppMessage</param>
86-
public void onWillDisplayInAppMessage(AndroidJavaObject message) {
87-
_parent.WillDisplay?.Invoke(message.ToSerializable<InAppMessage>());
88-
}
86+
public void onWillDisplayInAppMessage(AndroidJavaObject message)
87+
=> UnityMainThreadDispatch.Post(state => _parent.WillDisplay?.Invoke(message.ToSerializable<InAppMessage>()));
88+
8989
/// <param name="message">IInAppMessage</param>
9090
public void onDidDisplayInAppMessage(AndroidJavaObject message)
91-
=> _parent.DidDisplay?.Invoke(message.ToSerializable<InAppMessage>());
91+
=> UnityMainThreadDispatch.Post(state => _parent.DidDisplay?.Invoke(message.ToSerializable<InAppMessage>()));
9292

9393
/// <param name="message">IInAppMessage</param>
9494
public void onWillDismissInAppMessage(AndroidJavaObject message)
95-
=> _parent.WillDismiss?.Invoke(message.ToSerializable<InAppMessage>());
95+
=> UnityMainThreadDispatch.Post(state => _parent.WillDismiss?.Invoke(message.ToSerializable<InAppMessage>()));
9696

9797
/// <param name="message">IInAppMessage</param>
9898
public void onDidDismissInAppMessage(AndroidJavaObject message)
99-
=> _parent.DidDismiss?.Invoke(message.ToSerializable<InAppMessage>());
99+
=> UnityMainThreadDispatch.Post(state => _parent.DidDismiss?.Invoke(message.ToSerializable<InAppMessage>()));
100100
}
101101

102102
private sealed class IInAppMessageClickHandler : OneSignalAndroidJavaProxy {
@@ -107,9 +107,8 @@ public IInAppMessageClickHandler(AndroidInAppMessagesManager inAppMessagesManage
107107
}
108108

109109
/// <param name="result">IInAppMessageClickResult</param>
110-
public void inAppMessageClicked(AndroidJavaObject result) {
111-
_parent.Clicked?.Invoke(result.ToSerializable<InAppMessageClickedResult>());
112-
}
110+
public void inAppMessageClicked(AndroidJavaObject result)
111+
=> UnityMainThreadDispatch.Post(state => _parent.Clicked?.Invoke(result.ToSerializable<InAppMessageClickedResult>()));
113112
}
114113
}
115114
}

com.onesignal.unity.android/Runtime/AndroidNotificationsManager.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public async Task ClearAllNotificationsAsync() {
5959
}
6060

6161
public void Initialize() {
62-
_notifications.Call("addPermissionChangedHandler", new IPermissionChangedHandler(this)); // Notifications.PermissionChanged
62+
_notifications.Call("addPermissionChangedHandler", new IPermissionChangedHandler(this));
6363
_notifications.Call("setNotificationWillShowInForegroundHandler", new INotificationWillShowInForegroundHandler(this));
6464
_notifications.Call("setNotificationClickHandler", new INotificationClickHandler(this));
6565
}
@@ -73,7 +73,7 @@ public IPermissionChangedHandler(AndroidNotificationsManager notificationsManage
7373

7474
/// <param name="permission">boolean</param>
7575
public void onPermissionChanged(bool permission) {
76-
_parent.PermissionChanged?.Invoke(permission);
76+
UnityMainThreadDispatch.Post(state => _parent.PermissionChanged?.Invoke(permission));
7777
}
7878
}
7979

@@ -94,9 +94,8 @@ public void notificationWillShowInForeground(AndroidJavaObject notificationRecei
9494
}
9595

9696
var notification = _getNotification(notifJO);
97-
var resultNotif = _parent.WillShow(notification);
9897

99-
notificationReceivedEvent.Call("complete", resultNotif != null ? notifJO : null);
98+
UnityMainThreadDispatch.Post(state => notificationReceivedEvent.Call("complete", _parent.WillShow(notification) != null ? notifJO : null));
10099
}
101100
}
102101

@@ -120,7 +119,7 @@ public void notificationClicked(AndroidJavaObject result) {
120119
action = action
121120
};
122121

123-
_parent.Clicked?.Invoke(notifClickResult);
122+
UnityMainThreadDispatch.Post(state => _parent.Clicked?.Invoke(notifClickResult));
124123
}
125124
}
126125

@@ -144,7 +143,7 @@ private static Notification _getNotification(AndroidJavaObject notifJO) {
144143

145144
private static NotificationAction _getAction(AndroidJavaObject actionJO) {
146145
var action = actionJO.ToSerializable<NotificationAction>();
147-
action.actionID = actionJO.Call<string>("getActionId");
146+
//action.actionID = actionJO.Call<string>("getActionId");
148147

149148
return action;
150149
}

0 commit comments

Comments
 (0)