12

I am trying to create a method that schedules a notification. Within that method, I initialize an AlarmManager which will allow me to receive Intents on demand. However, I get the following syntax error in my code:

Type mismatch. Required: Context Found: String

On the following line:

 var alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager

Below is my code:

package com.example.notificationapp

import android.app.AlarmManager
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.PendingIntent.getActivity
import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat.getSystemService
import java.security.AccessController.getContext
import java.util.*

// Channel

class Notification(context:Context, notificationManager: NotificationManager, title:String, description:String, date: Date) {
    // Attributes
    private lateinit var context:Context;
    private var title:String = ""
    private var description:String = ""
    private lateinit var date:Date;
    private lateinit var notificationManager:NotificationManager;

    // Initialization
    init {
        // Download the constructor parameters into the object's attributes
        this.context = context
        this.title = title
        this.description = description
        this.date = date
        this.notificationManager = notificationManager
    }

    // Method to set the notification at a specific time
    fun setNotificationAtTime(time:Date) {
        var notificationIntent = Intent(this.context, NotificationBroadcast::class.java)
        var pendingNotificationIntent = PendingIntent.getBroadcast(this.context,
            0, notificationIntent, 0)

        // Initialize an AlarmManager that allows us to receive intents on demand
        var alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager

        val builder = NotificationCompat.Builder(context, "com.example.notificationapp")
    }
}

2 Answers 2

15

It's because you are using ContextCompat.getSystemService instead of Context.getSystemService.

So you have two options here:

  1. Use ContextCompat.getSystemService using the right signature:
getSystemService(context, AlarmManager::class.java)
  1. Use Context.getSystemService removing the import statement:
androidx.core.content.ContextCompat.getSystemService
Sign up to request clarification or add additional context in comments.

2 Comments

Android and a never-ending fight with its APIs.
Hi, welcome to 2024. I'm reading this official documentation developer.android.com/training/monitoring-device-state/… and I'm reproducing this code: pastebin.com/DPxBguHr and Context does not longer have getSystemService() Context.getSystemService() does not work. So, how can I make it work now? Api 24
14

add requireActivity() before the call to getSystemService()

val alarmManager = requireActivity().getSystemService(Context.ALARM_SERVICE) as AlarmManager

1 Comment

This one worked for me inside an android fragment. Thx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.