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