android - 在Android中单击通知时如何打开应用程序?

标签 android android-studio kotlin

我已经发出了一个无意向MainActivity的通知,它可以正常工作,但是当我将该意图添加到MainActivity时,该通知不再显示。我的代码有什么问题吗?我是否需要更改 list 或在MainActivity中添加一些代码?

这是我的代码。我将其设置为两个函数-setDailyRemindershowAlarmNotification

fun setDailyReminder(context: Context, type: String, message: String) {
    val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
    val intent = Intent(context, MainActivity::class.java)
    intent.putExtra(EXTRA_MESSAGE, message)
    intent.putExtra(EXTRA_TYPE, type)
    val timeArray =
        TIME_DAILY.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
    val calendar = Calendar.getInstance()
    calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeArray[0]))
    calendar.set(Calendar.MINUTE, Integer.parseInt(timeArray[1]))
    calendar.set(Calendar.SECOND, 0)
    val pendingIntent = PendingIntent.getBroadcast(context,
        ID_DAILY, intent, 0)
    alarmManager.setInexactRepeating(
        AlarmManager.RTC_WAKEUP,
        calendar.timeInMillis,
        AlarmManager.INTERVAL_DAY,
        pendingIntent
    )
    Toast.makeText(context, "Daily reminder set up", Toast.LENGTH_SHORT).show()
}

private fun showAlarmNotification(
    context: Context,
    title: String,
    message: String?,
    notifId: Int
) {

    val CHANNEL_ID = "Github App"
    val CHANNEL_NAME = "Let's find favourite user on Github"

    val notificationManagerCompat =
        context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    val alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    val builder = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_play_circle_filled_white_24dp)
        .setContentTitle(title)
        .setContentText(message)
        .setSound(alarmSound)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(
            CHANNEL_ID,
            CHANNEL_NAME,
            NotificationManager.IMPORTANCE_DEFAULT
        )
        builder.setChannelId(CHANNEL_ID)
        notificationManagerCompat.createNotificationChannel(channel)
    }
    val notification = builder.build()
    notification.flags = notification.flags or Notification.FLAG_AUTO_CANCEL
    notificationManagerCompat.notify(notifId, notification)
}

最佳答案

 fun showNotification(context: Context,title: String, message:String, notifId: Int){

       createNotificationChannel(context)

       val intent = Intent(context, MainActivity::class.java)
       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
       val pendingIntent = PendingIntent.getActivity(this, 0, 
       intent,PendingIntent.FLAG_ONE_SHOT)

        var  builder = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_play_circle_filled_white_24dp)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setColor(resources.getColor(R.color.colorAccent))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)

        val notificationManagerCompat = NotificationManagerCompat.from(this)
        notificationManagerCompat.notify(notifId, builder.build())
    }

    private fun createNotificationChannel(context: Context) {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name = "Test"
            val descriptionText = "FCM"
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
                description = descriptionText
            }
            // Register the channel with the system
            val notificationManager: NotificationManager =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }

关于android - 在Android中单击通知时如何打开应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62042441/

相关文章:

kotlin - android - 使用 Kotlin 改造调用无效

android - Gradle找不到kotlin编译器版本

android - 图库小部件的自定义边框

java - 当我发送推送时 android 应用程序崩溃

Android Eclipse 到 Android Studio ---> 项目结构

android - 为什么 Android Studio 不断切换 `languageLevel` ?

android - 如何使用 Android Native OpenCV 在纵向 View 中使用前置摄像头

java - 如何将单个字段的数据获取到 SQLite 数据库的变量中?

android - 在模块classes.jar中找到重复的类android.support.v4.app.NotificationCompat$Action$Extender (com.android.support :support

java - 无法将Android项目提交或推送到github,“提交”按钮被盗:(