0

I have a class that extends Firebase Messaging service:

internal class The8AppGCMListenerService : FirebaseMessagingService() {

override fun onNewToken(token: String) {
    super.onNewToken(token)
   
//save token

}

override fun onMessageReceived(message: RemoteMessage) {

        val from = message.from
        val data = message.data

        sendNotification(data)

    
}

private fun sendNotification(data: Map<String, String>) {
    if (Interactors.preferences.notificationsEnabled == true) {

        Timber.d(data.toString())
        val msg = data[KEY_MSG]
        val url = data[KEY_URL]

        sendNotification(msg, type, url)
    }
}

private fun sendNotification(message: 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)


        notificationChannel.description = "Channel description"
        notificationChannel.enableLights(true)
        notificationChannel.lightColor = Color.RED
        notificationChannel.vibrationPattern = longArrayOf(0, 1000, 500, 1000)
        notificationChannel.enableVibration(true)
        notificationManager.createNotificationChannel(notificationChannel)
    }

    val pendingIntent = getNotificationIntent(url)//Build intent with data received from notifcation
    val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    val notificationBuilder = NotificationCompat.Builder(this, channelId)
    notificationBuilder.setSmallIcon(R.drawable.ic_notification)
    notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.notif))
    notificationBuilder.setContentTitle(getString(R.string.app_name))
    notificationBuilder.setContentText(message)
    notificationBuilder.setAutoCancel(true)
    notificationBuilder.setSound(defaultSoundUri)
    notificationBuilder.setContentIntent(pendingIntent)
    notificationManager.notify(0, notificationBuilder.build())
}

private fun getNotificationIntent(Long, url: String?): PendingIntent {


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

    return PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_SINGLE_TOP)
}


companion object {

    private const val KEY_MSG = "Text"
    private const val KEY_URL = "pushUrl"

    internal fun getNotificationIntent(context: Context, useState: UseState, url: String?): Intent =

           
             openSponsorTree(context,useState,ASponsorTree.TAB_FEED,url)
        

        }.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).apply {
            if (The8CloudSdk.isSdkReady()) {
                RxBus.post(SponsorHubEvents.UpdateNotifications())
                SDKInternal.notifyBadgeListener()
            }
        }

    private fun openSponsorTree(context: Context, useState: UseState, startTab: Int, url: String?): Intent {
        val intent = ASponsorTree.newInstance(context, useState, startTab,url=url)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
        return intent
    }
}

}

I've declared it in the manifest like so:

<service android:name="com.weare8.android.network.gcm.The8AppGCMListenerService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_notification" />

    <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/app_dark_blue" />

And imported these firebase packages:

implementation('com.google.firebase:firebase-core:18.0.2')
implementation platform('com.google.firebase:firebase-bom:26.4.0')
implementation 'com.google.firebase:firebase-messaging-ktx'
implementation 'com.google.firebase:firebase-analytics-ktx'

However, when the notification is clicked when the app is in the background all it does is open the app and does not parse the data returned from message.data in onMessageReceived(). I can confirm this when I attempt to get the URL passed into the ASponsorTree activity and it is null.

How do I pass data into the app when bringing it from the background via a notification?

Edit: newInstance() method of ASponsorTree activity:

companion object {
 
    const val KEY_URL = "pushUrl"


    fun newInstance(
        context: Context,
        useState: UseState,
        startTab: Int = TAB_FEED,
        url:String?,
        noPost: String? = null
    ): Intent {

            val intent = Intent(context, ASponsorTree::class.java).putExtra(KEY_URL,url)
           

        return intent
    }
}

I retrieved it in that activity's oncreate with

val pushUrl = intent.getStringExtra(KEY_URL)

which evaluates to null.

andrewedgar
  • 797
  • 1
  • 12
  • 46
  • You can use intent.putExtra(key, value) to send extra data with intent. Then on receiving end just use intent.getStringExtra(key, default_value) if you put String, also you can use it for other types of variables. – SlothCoding Jan 29 '21 at 21:13
  • Edited my post. I do do that, but it is null when the activity starts – andrewedgar Jan 29 '21 at 21:37
  • I don't get this. You are using this line: sendNotification(msg, type, URL), what is the "type" in here? I don't see anything inside that function? Also, you don't have sendNotification() function with 3 parameters. I am not really great with Kotlin but how does this even work? – SlothCoding Jan 29 '21 at 21:42
  • And also, inside object companion, you create newInstance with url:String? = null, which then you use to putExtra under KEY_URL as key? Am I missing something here? Sorry for all this but I am pretty tired but also I want to help you. – SlothCoding Jan 29 '21 at 21:43
  • @SlothCoding The extra parameters were a mistake, forgot to remove them when I was cleaning up the code. I believe url parameter is given null as its default value but then takes the value passed, but just in case I removed null and just made it nullable and its still doing the same thing. Edited. – andrewedgar Jan 29 '21 at 21:59
  • Sorry, this shows how tired I am. I think you made this more complicated than it really is and I am not that great with Kotlin to be able to help you. You probably did a lot of edits in your code because here functions are called with the wrong parameters and what else. What is ASponsorTree::class.java? – SlothCoding Jan 29 '21 at 22:11
  • Also, when is openSponsorTree called? Why not use there intent.putExtra? – SlothCoding Jan 29 '21 at 22:16
  • It's the activity where I need the extra. It's called in getNotificationIntent – andrewedgar Jan 29 '21 at 22:21

1 Answers1

0

I learned that onMessageReceived() is not called from the background unless the a data message is used, and that the receiving activity must be tagged with a click action in the manifest.

andrewedgar
  • 797
  • 1
  • 12
  • 46