【问题标题】:Open activity after click in notification in alarmmanager and broadcast Kotlin在alarmmanager中点击通知后打开活动并广播Kotlin
【发布时间】:2020-02-11 10:28:03
【问题描述】:

我以这种方式在我的应用程序上每 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
        )

使用此类 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 小时后到达通知等等。我希望当通知到达时我可以点击它并打开应用程序(可能在我当前的家庭活动中),所以这个警报希望在 7 小时后自动设置。 我看到我必须用我的家庭意图修改pendingIntent......但我有一个

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

需要调用Alarm Receiver 类。

谁能帮帮我?

【问题讨论】:

    标签: android kotlin push-notification notifications alarmmanager


    【解决方案1】:

    您只需在通知生成器中添加.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。

    【讨论】:

    • 我在我的 Builder 中使用了你的 setContentIntet!感谢您在这方面为我提供更多帮助!
    • @theantomc 如果我的回答对您有帮助,请接受它作为对您问题的回答。是的,当然,如果可以的话,我可以帮助您解决任何问题。谢谢
    • @theantomc 如果您觉得这有帮助,请记得点赞或标记为已回答stackoverflow.com/help/someone-answers
    【解决方案2】:

    您需要创建和设置待处理的意图 -

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

    希望这对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多