Reputation: 1928
The click redirection to NotificationMainActivity doesn't function properly when the application is in the background or cleared from the recent applications. While the app is in the foreground, clicking on the notification successfully redirects to NotificationMainActivity. However, when the user clears the application or it is in the background, the click doesn't work.
My application has a specific requirement where users should be able to click on the notification and directly open a specific activity class, even if the app is on the locked screen. I initially used PendingIntent.getBroadcast, which worked fine in previous SDK versions. However, for SDK versions greater than 30, this method doesn't work as expected.
Even after attempting to use PendingIntent.getActivity to address the issue, it still doesn't fulfill my requirement. The problem persists because when implemented, it prompts the user to enter their PIN when they are on the lock screen. Hence, utilizing PendingIntent.getActivity doesn't resolve the given scenario.
I need a solution to address this issue. Below is the code snippet for reference:
NotificationUtils.kt
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.graphics.Color
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import test.example.application.R
import test.example.application.ui.NotificationMainActivity
class NotificationUtils(base: Context?) : ContextWrapper(base) {
private var mManager: NotificationManager? = null
init {
createChannels()
}
private fun createChannels() {
// Create android channel
val androidChannel = NotificationChannel(
ANDROID_CHANNEL_ID,
ANDROID_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT
)
androidChannel.enableLights(true)
androidChannel.enableVibration(true)
androidChannel.lightColor = Color.GREEN
androidChannel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
manager!!.createNotificationChannel(androidChannel)
}
val manager: NotificationManager?
get() {
if (mManager == null) {
mManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
}
return mManager
}
fun showCustomNotification(context: Context) {
val notificationBuilder = getAndroidChannelNotification(context)
// Get the PendingIntent for the notification click
val piView = getViewPendingIntent(context, TYPE_VIEW_CODE)
// Set the PendingIntent to the notification builder
notificationBuilder.setContentIntent(piView)
// Show the notification
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(0, notificationBuilder.build())
}
private fun getAndroidChannelNotification(context: Context): NotificationCompat.Builder {
return NotificationCompat.Builder(applicationContext, ANDROID_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_logo_for_notification)
.setAutoCancel(false)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.notification_pull_down_information))
.setShowWhen(false)
.setColor(Color.BLACK)
.setAutoCancel(true)
.setContent(getCustomNotificationView(context))
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
}
private fun getCustomNotificationView(context: Context): RemoteViews {
val notificationView = RemoteViews(context.packageName, R.layout.custom_notification_layout)
notificationView.setTextViewText(R.id.notification_title, context.getString(R.string.in_case))
notificationView.setTextViewText(
R.id.notification_data,
context.getString(R.string.notification_pull_down_information)
)
return notificationView
}
private fun getViewPendingIntent(context: Context, typeViewCode: Int): PendingIntent {
val viewIntent = Intent(context, NotificationReceiver::class.java)
viewIntent.putExtra(CODE, TYPE_VIEW_CODE)
return PendingIntent.getBroadcast(
context,
typeViewCode,
viewIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
}
companion object {
const val TYPE_VIEW_CODE = 987
const val CODE = "Code"
const val ANDROID_CHANNEL_ID = "test.example.application.ANDROID"
const val ANDROID_CHANNEL_NAME = "ANDROID CHANNEL"
}
}
NotificationReceiver.kt
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import test.example.application.receiver.NotificationUtils.Companion.CODE
import test.example.application.receiver.NotificationUtils.Companion.TYPE_VIEW_CODE
import test.example.application.ui.NotificationMainActivity
class NotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val intentCode = intent.getIntExtra(CODE, 0)
when (intentCode) {
TYPE_VIEW_CODE -> {
Log.d("VIEWCODE", "$intentCode")
val viewIntent = Intent(context, NotificationMainActivity::class.java)
viewIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
context.startActivity(viewIntent)
}
}
}
}
AndroidManifest.xml
<receiver
android:name=".receiver.NotificationReceiver"
/>
Please assist me in solving this problem.
Upvotes: 0
Views: 131
Reputation: 1
Make sure you are not also declaring NotificationReceiver dynamically which will tie Broadcast receivers lifecycle with your application
Upvotes: -1