Tuesday, 21 September 2021

Check payload of notification in onMessageReceived()

I am trying to send a data notification to my app and then use the data therein to open a fragment:

override fun onMessageReceived(message: RemoteMessage) {

    Timber.d("onMessageReceived")

    try {

        val data = message.data

        if (data != null && (data.containsKey(KEY_MSG) || data.containsKey(KEY_URL))) {

            val url = data[KEY_URL]

            if (!url.isNullOrEmpty()) {
                val clickAction = message.notification?.clickAction
                val intent = Intent(clickAction)
                intent.putExtra(KEY_URL, url).putUseStateExtra(UseState(UseState.COME_FROM_NOTIFICATION)).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
            } else {
                sendNotification(data)
            }


        }
    } catch (e: Throwable) {
        Timber.e(e, "We didn't send the notification because ${e.message}")
    }
}

I then after onMessageReceived() is called I build the notification and send it with the following methods. One to parse out the payload:

private fun sendNotification(data: Map<String, String>) {

    Timber.d("Notification sent: $data type: ${data[KEY_CLOUD8_TYPE]}")

    if (Interactors.preferences.notificationsEnabled == true) {

        Timber.d(data.toString())
        val title = data[KEY_TITLE]
        val msg = data[KEY_MSG]
        var cloud8Type = data[KEY_CLOUD8_TYPE] ?: ""
        var notificationType = data[NOTIFICATION_TYPE] ?: ""
        val campaignId = (data[KEY_CAMPAIGN_ID] ?: "0")
        val url = data[KEY_URL]

        if (!url.isNullOrBlank()) {
            cloud8Type = Cloud8Type.Link
        }
        sendNotification(title, msg, cloud8Type, notificationType, campaignId, url)
    }
}

One to build the notification:

private fun sendNotification(title: String?, message: String?, cloud8Type: String, notificationType: String, offerId: String, url: String?) {
    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    val channelId = "Main"

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val notificationChannel = NotificationChannel(channelId, "My Notifications", NotificationManager.IMPORTANCE_HIGH)

        // Configure the notification channel.
        notificationChannel.description = "Channel description"
        notificationChannel.enableVibration(true)
        notificationManager.createNotificationChannel(notificationChannel)
    }

    val pendingIntent = getNotificationIntent(cloud8Type, notificationType, offerId, url)
    val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    val notificationBuilder = NotificationCompat.Builder(this, channelId)
    notificationBuilder.setSmallIcon(R.drawable.ic_notification)
    notificationBuilder.setContentTitle(title)
    notificationBuilder.setContentText(message)
    notificationBuilder.setAutoCancel(true)
    notificationBuilder.setSound(defaultSoundUri)
    notificationBuilder.setContentIntent(pendingIntent)
    notificationManager.notify(0, notificationBuilder.build())
}

And two to use the content of the payload to build an intent:

private fun getNotificationIntent(cloud8Type: String, notificationType: String, offerId: String, url: String?): PendingIntent {

    Timber.d("Notification type: $cloud8Type}")

    val useState = UseState(UseState.COME_FROM_NOTIFICATION)
    val intent = getNotificationIntent(this, cloud8Type, notificationType, useState, offerId, url = url)

    return PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}

internal fun getNotificationIntent(
        context: Context,
        cloud8Type: String?,
        notificationType: String,
        useState: UseState,
        offerId: String?,
        url: String?
    ): Intent {

        var intent = Intent()
        when (cloud8Type) {
            Cloud8Type.NewOffer, Cloud8Type.NewChallengeOffer, Cloud8Type.Link ->
                intent = StartActivity.newInstance(context, useState, offerId, url)

            Cloud8Type.DailyEarning, Cloud8Type.YouDidIt, Cloud8Type.FundsTransfered, Cloud8Type.OfferPayment, Cloud8Type.OfferDonation -> {
                intent = if (Interactors.preferences.the8CloudSdkInfo.showPayoutTab) {
                    openSponsorTree(context, useState, ASponsorTree.TAB_PAYOUT, null)
                } else {
                    APayoutMain.newIntent(context)
                }
            }
            Cloud8Type.NewOffers ->
                intent = openSponsorTree(context, useState, ASponsorTree.TAB_FEED, null)


            else -> {
                when (notificationType) {
                    NotificationType.Payment -> intent = openSponsorTree(context, useState, ASponsorTree.TAB_PAYOUT, null)
                }
            }
        }



        return intent
    }

I'm trying to debug the payload being received when the notification comes, but none of my log statements are showing up when the app is closed. Is there any way to see what is coming back with the RemoteMessage in onMessageReceived()? Is there anyting else I should know about how to accomplish what I want to accomplish?



from Check payload of notification in onMessageReceived()

No comments:

Post a Comment