Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a7a56fc

Browse files
MBX-3675: run handleRemoteMessage asynchronously in example
1 parent 608a59d commit a7a56fc

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

example/flutter_example/android/app/src/main/kotlin/cloud/mindbox/flutter_example/MindboxHuaweiMessagingService.kt

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ package cloud.mindbox.flutter_example
33
import cloud.mindbox.mobile_sdk.Mindbox
44
import com.huawei.hms.push.*
55
import cloud.mindbox.mindbox_huawei.MindboxHuawei
6+
import kotlinx.coroutines.*
67

78
class MindboxHuaweiMessagingService: HmsMessageService() {
9+
10+
private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
11+
812
override fun onNewToken(token: String) {
913
super.onNewToken(token)
1014
Mindbox.updatePushToken(applicationContext, token, MindboxHuawei)
@@ -17,31 +21,40 @@ class MindboxHuaweiMessagingService: HmsMessageService() {
1721
val channelDescription = "default_channel_description"
1822
val pushSmallIcon = android.R.drawable.ic_dialog_info
1923

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+
}
4453
}
54+
}
4555

56+
override fun onDestroy() {
57+
super.onDestroy()
58+
coroutineScope.cancel()
4659
}
4760
}

0 commit comments

Comments
 (0)