在 alarmmanager 中点击 notification 并广播 Kotlin 后打开 activity

Open activity after click in notification in alarmmanager and broadcast Kotlin

我以这种方式在我的应用程序上设置了每 7 小时通知一次:

  alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
        val intent = Intent(this, AlarmBroadcastReceiver::class.java)
        pendingIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT)

        // Setting the specific time for the alarm manager to trigger the intent, in this example, the alarm is set to go off at 23:30, update the time according to your need
        val calendar = Calendar.getInstance()

        val next=  calendar.get(Calendar.HOUR_OF_DAY) + 7

        calendar.timeInMillis = System.currentTimeMillis()
        calendar.set(Calendar.HOUR_OF_DAY, next)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.SECOND,  0)

        // Starts the alarm manager
        alarmManager.setRepeating(
                AlarmManager.RTC_WAKEUP,
                calendar.timeInMillis,
                AlarmManager.INTERVAL_DAY,
                pendingIntent
        )

用这个 class AlarmBroadcastReceiver :

class AlarmBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            // Create the NotificationChannel
            val name = "Alarme"
            val descriptionText = "Detalhes do Alarme"
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val mChannel = NotificationChannel("AlarmId", name, importance)
            mChannel.description = descriptionText
            val notificationManager = context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(mChannel)
        }

        // Create the notification to be shown
        val mBuilder = NotificationCompat.Builder(context!!, "AlarmId")
                .setSmallIcon(R.mipmap.ic_food)
                .setContentTitle("Synchronize Fitbit")
                .setContentText("Synchronize Fitbit data and log-in SugarFree for don't lose daily data")
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)

        // Get the Notification manager service
        val am = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        // Generate an Id for each notification
        val id = System.currentTimeMillis() / 1000

        // Show a notification
        am.notify(id.toInt(), mBuilder.build())

通知效果很好,我进入应用程序,设置闹钟,7 小时后到达通知等等。我希望当通知到达时我可以点击它并打开应用程序(也许在我现在的家里 activity)所以这个闹钟希望在 7 小时后自动设置。 我看到我必须用我的家庭意图修改 pendingIntent...但我有一个

val intent = Intent(this, AlarmBroadcastReceiver::class.java)

需要调用报警接收器class。

谁能帮帮我?

您只需在通知生成器中添加 .setContentIntent

// Create the notification to be shown
        val mBuilder = NotificationCompat.Builder(context!!, "AlarmId")
                .setSmallIcon(R.mipmap.ic_food)
                .setContentTitle("Synchronize Fitbit")
                .setContentText("Synchronize Fitbit data and log-in SugarFree for don't lose daily data")
                .setAutoCancel(true)
                .setContentIntent(PendingIntent.getActivity(
                    context, // Context from onReceive method.
                    0,
                    Intent(context, HomeActivity::class.java), // Activity you want to launch onClick.
                    0
                  )
                )
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .build()

您可以找到更多信息 here 文档提供了有关如何处理任务的更多信息,例如一次只打开一个 Activity。

您需要创建并设置待定意向 -

// Create an Intent for the activity you want to start
val resultIntent = Intent(this, YourMainActivity::class.java)
// Create the TaskStackBuilder
val resultPendingIntent: PendingIntent? = TaskStackBuilder.create(this).run {
    // Add the intent, which inflates the back stack
    addNextIntentWithParentStack(resultIntent)
    // Get the PendingIntent containing the entire back stack
    getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
} 

然后设置 -

val builder = NotificationCompat.Builder(this, CHANNEL_ID).apply {
    setContentIntent(resultPendingIntent)
    ...
}
with(NotificationManagerCompat.from(this)) {
    notify(NOTIFICATION_ID, builder.build())
}

希望这对你有用。