I am currently working on a schedule reminder feature for my app which can send reminders to your phone at a selected time via push notifications. I’ve written out the code in the following Kotlin class labelled NotificationsWorker.kt:
package com.example.project.ui.screens
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.work.Worker
import androidx.work.WorkerParameters
class NotificationWorker(context: Context, workerParams: WorkerParameters) :
Worker(context, workerParams) {
override fun doWork(): Result {
val title = inputData.getString("title") ?: return Result.failure()
Log.d("NotificationWorker", "doWork called. Title: $title")
showNotification(title)
return Result.success()
}
private fun showNotification(title: String) {
val notificationManager =
applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel =
NotificationChannel("reminders", "Reminders", NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(channel)
}
val notification = NotificationCompat.Builder(applicationContext, "reminders")
.setContentTitle("Reminder")
.setContentText(title)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.build()
notificationManager.notify(System.currentTimeMillis().toInt(), notification)
}
}
But for some reason, the push notifications aren’t appearing in the emulator (The ‘Running Devices’). The emulator that I’m using is ‘Medium Phone API 35’.
I’ve written out the code:
override fun doWork(): Result {
val title = inputData.getString("title") ?: return Result.failure()
Log.d("NotificationWorker", "doWork called. Title: $title")
showNotification(title)
return Result.success()
}
so it’ll appear in the Logcat log viewer window, and when I sending a push notification in the emulator, I get the ‘doWork called. Title: test‘ message in the log, which means it knows that a notification has been received, but it’s not showing up in the emulator.
What can I do to make the emulator receive push notifications so I know that the reminder has been received?