We noticed that if we send a message while the app is running, the SDK does not try to handle it until the app is put in background and back to foreground. Our question is if there is a way to let the in-app showing when the app is already in foreground.
The app is currently using com.salesforce.marketingcloud:marketingcloudsdk:7.1.0
.
The following is a snippet of our FirebaseMessagingService
class that logs the messages we receive:
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Timber.tag(NotificationUtils.TAG_NOTIFICATION_LOG).d(
"onMessageReceived: title=${remoteMessage.notification?.title}, body=${remoteMessage.notification?.body}, data=${remoteMessage.data}",
)
// Some more code to handle app specific notifications
}
The following is what we have in our Application
class to initialize the SDK:
override fun onCreate() {
super.onCreate()
// Some initialization code
FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener {
MarketingCloudSdk.init(this, MarketingCloudConfig.builder().apply {
setApplicationId(BuildConfig.MARKETINGCLOUD_ID_APP)
setAccessToken(BuildConfig.MARKETINGCLOUD_ACCESS_TOKEN)
setSenderId(it.token)
setMarketingCloudServerUrl(BuildConfig.MARKETINGCLOUD_ENDPOINT)
setMid(BuildConfig.MARKETINGCLOUD_MID)
setNotificationCustomizationOptions(
NotificationCustomizationOptions.create(R.drawable.tpx_notification_app_icon)
)
setUrlHandler { context, url, _ ->
PendingIntent.getActivity(
context,
Random().nextInt(),
// The notification is handled only if the user is already logged in,
// so we can jump straight to HomeActivity.
Intent(context, HomeActivity::class.java).apply {
putExtra(Constants.NOTIFICATION_NOTIFICATION_TYPE, Constants.NOTIFICATION_LINK)
putExtra(Constants.NOTIFICATION_LINK, url)
},
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
}
}.build(this as Context)) {
LogUtils.d("MarketingCloudSdk initialized")
}
MarketingCloudSdk.requestSdk { sdk ->
sdk.inAppMessageManager.setInAppMessageListener(
object : InAppMessageManager.EventListener {
override fun shouldShowMessage(message: InAppMessage): Boolean {
return if (!NotificationUtils.canHandleMarketingNotifications) {
Timber.tag(NotificationUtils.TAG_NOTIFICATION_LOG)
.d("shouldShowMessage: Queuing notification id=%s", message.id)
marketingCloudStorage.saveMessageId(message.id)
false
} else {
Timber.tag(NotificationUtils.TAG_NOTIFICATION_LOG)
.d("shouldShowMessage: Handling notification id=%s", message.id)
marketingCloudStorage.clearMessageId()
true
}
}
override fun didShowMessage(message: InAppMessage) {
Timber.tag(NotificationUtils.TAG_NOTIFICATION_LOG)
.d("didShowMessage: id=%s", message.id)
}
override fun didCloseMessage(message: InAppMessage) {
Timber.tag(NotificationUtils.TAG_NOTIFICATION_LOG)
.d("didCloseMessage: id=%s", message.id)
}
}
)
}
}
}
When the app receives a marketing cloud notification while it is running, we see the notification passing through our FirebaseMessagingService
:
11:06:13.695 onMessageReceived: title=null, body=null, data={subtitle=Test in-app, _h=wF0j2LQ3yYAslTxIdL2w3wAAAAAA, _m=MTUyMjoxMTQ6MA, _r=fb0e6dac-40cf-4766-b3c8-bd979a1f0a5d, _mt=1, _sid=SFMC, alert=Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor, sound=default, title=MK_TEST}
This tells us that the message has been received by the device. At this point we'd expect shouldShowMessage
to be called without doing anything if the app is currently in foreground, but that is not the case. We have to put the app in background and back to foreground for shouldShowMessage
to be called.
For example, here I waited few seconds (notice the timestamps) before putting the app in background and as soon as I brought the app in foreground, shouldShowMessage
was called. In this case we immediately handled the notification since the app was ready to handle it:
11:06:33.301 shouldShowMessage: Handling notification id=MTUyMToxMTQ6MDpORFNUbUItbUJrLXNYQzJ5bklWQk53
11:06:33.380 didShowMessage: id=MTUyMToxMTQ6MDpORFNUbUItbUJrLXNYQzJ5bklWQk53
Many thanks Alessandro