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.