@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
4
4
import android.content.Context
5
5
import android.location.Location
6
6
import android.os.Build
7
+ import android.os.Looper
7
8
import android.util.Log
8
9
import com.clevertap.android.sdk.CleverTapAPI
9
10
import com.clevertap.android.sdk.UTMDetail
@@ -24,13 +25,16 @@ import io.flutter.plugin.common.MethodChannel
24
25
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
25
26
import org.json.JSONException
26
27
import org.json.JSONObject
28
+ import java.util.concurrent.Executors
27
29
28
30
@Suppress(" DEPRECATION" )
29
31
class DartToNativePlatformCommunicator (
30
32
private val context : Context ,
31
33
private val cleverTapAPI : CleverTapAPI ?
32
34
) : MethodCallHandler {
33
35
36
+ private val notificationExecutor = Executors .newSingleThreadExecutor()
37
+
34
38
companion object {
35
39
const val TAG = " DartToNative"
36
40
@@ -990,29 +994,40 @@ class DartToNativePlatformCommunicator(
990
994
*/
991
995
private fun renderNotification (call : MethodCall , result : MethodChannel .Result ) {
992
996
val extras = call.argument<String >(" extras" )
993
- if (cleverTapAPI != null ) {
994
- val isSuccess: Boolean
995
- try {
996
- Log .d(TAG , " renderNotification Android" )
997
- val messageBundle = Utils .stringToBundle(extras)
998
- isSuccess = PushNotificationHandler .getPushNotificationHandler()
999
- .onMessageReceived(context, messageBundle, PushConstants .FCM .type)
1000
- if (isSuccess) {
1001
- result.success(null )
1002
- } else {
1003
- throw java.lang.Exception (" Unable to process notification rendering" )
997
+ if (cleverTapAPI == null ) {
998
+ result.error(TAG , ERROR_MSG , null )
999
+ }
1000
+ try {
1001
+ Log .d(TAG , " renderNotification Android" )
1002
+ // Check if we're on the main thread. Needed since plugins like FCM provide on message received on the main thread
1003
+ if (Looper .myLooper() == Looper .getMainLooper()) {
1004
+ // We're on the main thread, execute on a background thread
1005
+ notificationExecutor.execute {
1006
+ processNotification(extras, result)
1004
1007
}
1005
- } catch (e: JSONException ) {
1006
- result.error(
1007
- TAG ,
1008
- " Unable to render notification due to JSONException - " + e.localizedMessage,
1009
- null
1010
- )
1011
- } catch (e: java.lang.Exception ) {
1012
- result.error(TAG , e.localizedMessage, null )
1008
+ } else {
1009
+ processNotification(extras, result)
1013
1010
}
1011
+ } catch (e: JSONException ) {
1012
+ result.error(
1013
+ TAG ,
1014
+ " Unable to render notification due to JSONException - " + e.localizedMessage,
1015
+ null
1016
+ )
1017
+ } catch (e: Exception ) {
1018
+ result.error(TAG , e.localizedMessage, null )
1019
+ }
1020
+ }
1021
+
1022
+ private fun processNotification (extras : String? , result : MethodChannel .Result ) {
1023
+ val messageBundle = Utils .stringToBundle(extras)
1024
+ val isSuccess = PushNotificationHandler .getPushNotificationHandler()
1025
+ .onMessageReceived(context, messageBundle, PushConstants .FCM .type)
1026
+
1027
+ if (isSuccess) {
1028
+ result.success(null )
1014
1029
} else {
1015
- result.error(TAG , ERROR_MSG , null )
1030
+ result.error(TAG , " Unable to process notification rendering " , null )
1016
1031
}
1017
1032
}
1018
1033
0 commit comments