Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中单击通知时如何打开应用程序?_Android_Android Studio_Kotlin - Fatal编程技术网

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

在android中单击通知时如何打开应用程序?,android,android-studio,kotlin,Android,Android Studio,Kotlin,我已经发出了一个通知,没有MainActivity的意图,它工作正常,但是当我将该意图添加到MainActivity时,通知不再显示。我的代码是否有问题,或者我是否需要更改清单或在MainActivity中添加一些代码 这是我的密码。我将其设置为两个功能-setDailReminder和showAlarmNotification fun setDailyReminder(context: Context, type: String, message: String) { val alar

我已经发出了一个通知,没有MainActivity的意图,它工作正常,但是当我将该意图添加到MainActivity时,通知不再显示。我的代码是否有问题,或者我是否需要更改清单或在MainActivity中添加一些代码

这是我的密码。我将其设置为两个功能-
setDailReminder
showAlarmNotification

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)
}

当在另一个函数中,当悬而未决的意图时,如何获得悬垂的意图?@ RLVANTO,然后将值传递给ASKER而不是SpOnDebug代码,考虑解释您的代码是如何帮助解决ASKER的问题的。@ RLVANTO我已经纠正了代码。希望这对你有帮助。
 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)
        }
    }