AlarmManager, как открыть активность на кране? - PullRequest
0 голосов
/ 02 марта 2019

Я борюсь с этим уже 2-й день, и мне действительно нужна помощь

Вот как я запускаю уведомление:

object MyAlarmManager  {
//some stuff
private fun createNotification(title: String, calendar: Calendar){

        val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        val intent = Intent(context, AlertReceiver::class.java)
        val pendingIntent = PendingIntent.getBroadcast(context, 1, intent, 0)
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent);
    }
}

alertReceiver:

class AlertReceiver: BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
        val notificationHelper = NotificationHelper(context!!)
        val nb = notificationHelper.channelNotification
        notificationHelper.manager!!.notify(1, nb.build())
    }
}

ивот помощник

class NotificationHelper(private val base: Context) : ContextWrapper(base) {

    private var mManager: NotificationManager? = null

    val manager: NotificationManager?
        get() {
            if (mManager == null) {
                mManager = base.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            }

            return mManager
        }

    val channelNotification: NotificationCompat.Builder
        get() = NotificationCompat.Builder(applicationContext, channelID)
            .setContentTitle("Alarm!")
            .setContentText("Your AlarmManager is working.")
            .setSmallIcon(R.mipmap.ic_launcher)

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

    @TargetApi(Build.VERSION_CODES.O)
    private fun createChannel() {
        val channel = NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH)

        manager!!.createNotificationChannel(channel)
    }

    companion object {
        const val channelID = "channelID"
        const val channelName = "Channel Name"
    }
}

Теперь, пожалуйста, что мне нужно изменить в моем коде?Обратите внимание, что я не запускаю его из Activity.Я попытался добавить кое-что здесь и там, особенно val pendingIntent = PendingIntent.getBroadcast. Я переключился на .getActivity и передал контекст (из класса App), но это не сработало

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...