为什么无法点击我的 Android 通知进入我的应用程序?
why my Android notification can't be clicked to go inside my app?
我可以接收从 Firebase Cloud Messaging 发送的通知并在此处显示给 android 设备
当我点击来自我的应用程序的通知时,我希望它会进入我的应用程序。但是当我从我的应用程序中单击通知时,它什么也没做。好像不能点击
所以当我点击通知时如何让它进入我的应用程序内部。这是我使用的代码
使用此代码创建频道
class App : Application() {
override fun onCreate() {
super.onCreate()
createNotificationChannels()
}
private fun createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val infoChannel = NotificationChannel(
NOTIFICATION_CHANNEL_INFO,
NOTIFICATION_CHANNEL_INFO,
NotificationManager.IMPORTANCE_DEFAULT
)
val importantNews = NotificationChannel(
NOTIFICATION_EVENT_UPDATES,
NOTIFICATION_EVENT_UPDATES,
NotificationManager.IMPORTANCE_HIGH
)
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(infoChannel)
notificationManager.createNotificationChannel(importantNews)
}
}
}
并使用此代码构建通知
private fun setUpNotification(title: String, message: String, type: String, imagePath: String) {
val channel = if (type == "Event Updates") NOTIFICATION_EVENT_UPDATES else NOTIFICATION_CHANNEL_INFO
var notification = NotificationCompat.Builder(this, channel)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
if (type == "Event Updates") {
notification = notification
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_EVENT)
} else {
notification = notification
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
}
if (imagePath.isNotEmpty()) {
Glide.with(this)
.asBitmap()
.load(imagePath)
.into(object: SimpleTarget<Bitmap>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
notification = notification.setLargeIcon(resource)
val build = notification.build()
notificationManager.notify(1, build)
}
})
} else {
val build = notification.build()
notificationManager.notify(1, build)
}
}
}
你必须使用挂起的意图来实现这个
val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, Intent(context, TimerService::class.java), 0)
notification.setContentIntent(pendingIntent)
我可以接收从 Firebase Cloud Messaging 发送的通知并在此处显示给 android 设备
当我点击来自我的应用程序的通知时,我希望它会进入我的应用程序。但是当我从我的应用程序中单击通知时,它什么也没做。好像不能点击
所以当我点击通知时如何让它进入我的应用程序内部。这是我使用的代码
使用此代码创建频道
class App : Application() {
override fun onCreate() {
super.onCreate()
createNotificationChannels()
}
private fun createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val infoChannel = NotificationChannel(
NOTIFICATION_CHANNEL_INFO,
NOTIFICATION_CHANNEL_INFO,
NotificationManager.IMPORTANCE_DEFAULT
)
val importantNews = NotificationChannel(
NOTIFICATION_EVENT_UPDATES,
NOTIFICATION_EVENT_UPDATES,
NotificationManager.IMPORTANCE_HIGH
)
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(infoChannel)
notificationManager.createNotificationChannel(importantNews)
}
}
}
并使用此代码构建通知
private fun setUpNotification(title: String, message: String, type: String, imagePath: String) {
val channel = if (type == "Event Updates") NOTIFICATION_EVENT_UPDATES else NOTIFICATION_CHANNEL_INFO
var notification = NotificationCompat.Builder(this, channel)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
if (type == "Event Updates") {
notification = notification
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_EVENT)
} else {
notification = notification
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
}
if (imagePath.isNotEmpty()) {
Glide.with(this)
.asBitmap()
.load(imagePath)
.into(object: SimpleTarget<Bitmap>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
notification = notification.setLargeIcon(resource)
val build = notification.build()
notificationManager.notify(1, build)
}
})
} else {
val build = notification.build()
notificationManager.notify(1, build)
}
}
}
你必须使用挂起的意图来实现这个
val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, Intent(context, TimerService::class.java), 0)
notification.setContentIntent(pendingIntent)