Home > front end >  How to send a notification to the user once at a particular time?
How to send a notification to the user once at a particular time?

Time:02-15

The user should receive a notification at a specific date and time, but the notification just comes right away, instead of arriving when needed. How to make sure that the notification comes at the right time, and not immediately?

class MyReceiver: BroadcastReceiver() {

    private var title = ""
    private var desc = ""

    override fun onReceive(context: Context?, intent: Intent?) {

                try {
                    intent?.let {
                        title = it.getStringExtra(Constants.EXTRA_NOTIFICATION_TITLE).toString()
                        desc = it.getStringExtra(Constants.EXTRA_NOTIFICATION_MESSAGE).toString()
                    }
                }
                catch (e: Exception) {
                    Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
                    e.printStackTrace()
                }

        context?.let {
            showNotification(it, title, desc)
        }
    }

    private fun showNotification(context: Context, title: String, message: String) {
        val name = "Notification"
        val desc = "A Description of the Channel"

        val notificationManager =
            context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val builder: NotificationCompat.Builder

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationChannel =
                NotificationChannel(Constants.CHANNEL_ID, name, NotificationManager.IMPORTANCE_HIGH)
            notificationChannel.description = desc
            notificationChannel.enableLights(true)
            notificationChannel.lightColor = Color.CYAN
            notificationChannel.enableVibration(true)
            notificationChannel.vibrationPattern = longArrayOf(1000, 1000, 1000, 1000, 1000)
            notificationManager.createNotificationChannel(notificationChannel)

            builder = NotificationCompat.Builder(context, Constants.CHANNEL_ID)
                .setChannelId(Constants.CHANNEL_ID)
                .setContentTitle(title)
                .setContentText(message)
                .setSmallIcon(R.drawable.baseline_access_time_blue)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))
                .setContentIntent(
                    PendingIntent.getActivity(
                    context,
                    0,
                    Intent(context, MainActivity::class.java),
                    PendingIntent.FLAG_UPDATE_CURRENT
                    ))

        } else {
            builder = NotificationCompat.Builder(context, Constants.CHANNEL_ID)
                .setChannelId(Constants.CHANNEL_ID)
                .setContentTitle(title)
                .setContentText(message)
                .setSmallIcon(R.drawable.baseline_access_time_blue)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))
        }
        notificationManager.notify(Constants.NOTIFICATION_ID, builder.build())
    }
}

Call in activity:

private fun sendNotification() {
        val intent = Intent(applicationContext, MyReceiver::class.java)
        val title = etTask.text.toString()
        val message = etTime.text.toString()
        intent.putExtra(Constants.EXTRA_NOTIFICATION_TITLE, title)
        intent.putExtra(Constants.EXTRA_NOTIFICATION_MESSAGE, message)

        val pendingIntent = PendingIntent.getBroadcast(
            applicationContext,
            Constants.NOTIFICATION_ID,
            intent,
            PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
        )

        val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
        val time = getTime()
        alarmManager.setExactAndAllowWhileIdle(
            AlarmManager.RTC_WAKEUP,
            time,
            pendingIntent
        )
    }

Manifest:

<receiver android:name=".receiver.MyReceiver"
            android:enabled="true"/>

P.S. I don't know if it's better to use AlarmManager or WorkManager for this task.

CodePudding user response:

WorkManager is pretty much a standard way of doing anything schedule related

  • Related