@@ -3,8 +3,12 @@ package cloud.mindbox.flutter_example
3
3
import cloud.mindbox.mobile_sdk.Mindbox
4
4
import com.huawei.hms.push.*
5
5
import cloud.mindbox.mindbox_huawei.MindboxHuawei
6
+ import kotlinx.coroutines.*
6
7
7
8
class MindboxHuaweiMessagingService : HmsMessageService () {
9
+
10
+ private val coroutineScope = CoroutineScope (SupervisorJob () + Dispatchers .IO )
11
+
8
12
override fun onNewToken (token : String ) {
9
13
super .onNewToken(token)
10
14
Mindbox .updatePushToken(applicationContext, token, MindboxHuawei )
@@ -17,31 +21,40 @@ class MindboxHuaweiMessagingService: HmsMessageService() {
17
21
val channelDescription = " default_channel_description"
18
22
val pushSmallIcon = android.R .drawable.ic_dialog_info
19
23
20
- val messageWasHandled = Mindbox .handleRemoteMessage(
21
- context = applicationContext,
22
- message = remoteMessage,
23
- activities = mapOf (),
24
- channelId = channelId,
25
- channelName = channelName,
26
- pushSmallIcon = pushSmallIcon,
27
- defaultActivity = MainActivity ::class .java,
28
- channelDescription = channelDescription
29
- )
30
-
31
- // Method for checking if push is from Mindbox
32
- val isMindboxPush = MindboxHuawei .isMindboxPush(remoteMessage = remoteMessage)
33
-
34
- // Method for getting info from Mindbox push
35
- val mindboxMessage = MindboxHuawei .convertToMindboxRemoteMessage(remoteMessage = remoteMessage)
36
- // If you want to save the notification you can call your save function from here.
37
- mindboxMessage?.let {
38
- val app = applicationContext as MainApplication
39
- app.saveNotification(it)
40
- }
41
-
42
- if (! messageWasHandled) {
43
- // If the push notification was not from Mindbox or it contains incorrect data, then you can write a fallback to process it
24
+ // On some devices, onMessageReceived may be executed on the main thread
25
+ // We recommend handling push messages asynchronously
26
+ coroutineScope.launch {
27
+ val messageWasHandled = Mindbox .handleRemoteMessage(
28
+ context = applicationContext,
29
+ message = remoteMessage,
30
+ activities = mapOf (),
31
+ channelId = channelId,
32
+ channelName = channelName,
33
+ pushSmallIcon = pushSmallIcon,
34
+ defaultActivity = MainActivity ::class .java,
35
+ channelDescription = channelDescription
36
+ )
37
+
38
+ // Method for checking if push is from Mindbox
39
+ val isMindboxPush = MindboxHuawei .isMindboxPush(remoteMessage = remoteMessage)
40
+
41
+ // Method for getting info from Mindbox push
42
+ val mindboxMessage =
43
+ MindboxHuawei .convertToMindboxRemoteMessage(remoteMessage = remoteMessage)
44
+ // If you want to save the notification you can call your save function from here.
45
+ mindboxMessage?.let {
46
+ val app = applicationContext as MainApplication
47
+ app.saveNotification(it)
48
+ }
49
+
50
+ if (! messageWasHandled) {
51
+ // If the push notification was not from Mindbox or it contains incorrect data, then you can write a fallback to process it
52
+ }
44
53
}
54
+ }
45
55
56
+ override fun onDestroy () {
57
+ super .onDestroy()
58
+ coroutineScope.cancel()
46
59
}
47
60
}
0 commit comments