0

I am trying to run this function from within a fragment but i am having trouble with the context.

    private  fun createNotificationChannel(){
    val seconds = 3 *1000 // change the 3 to 31536000 when i need to hand in
    val intent = Intent(this, Receiver::class.java)
    val pendingIntent = PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT)
    Log.d("MainActivity", "Create : " + Date().toString())
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
        System.currentTimeMillis() + seconds,1000 * 60 * 1, pendingIntent)// change the 1 to 525600
    val CHANNEL_ID = "chanel_id_example_01"
    val notificationId = 101
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        val name = "Notifaction Title"
        val descriptionText = "Notification Description"
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
            description = descriptionText
        }
        val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }
Ryan M
  • 18,333
  • 31
  • 67
  • 74

1 Answers1

0

How to get the Context in a fragment

Call getContext() on the Fragment to get a Context (or null if the fragment is not attached to a Context at the present time).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491