Skip to content

Instantly share code, notes, and snippets.

View Lavanyagaur22's full-sized avatar
🚩

Lavanya gaur Lavanyagaur22

🚩
View GitHub Profile
val icon = BitmapFactory.decodeResource(
this.resources,
R.drawable.ic_launcher_foreground
)
val not = NotificationCompat.Builder(this, "first")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Hello There")
.setContentText("General Kenobi!")
.setLargeIcon(icon)
val bigTextStyle = NotificationCompat.Builder(this, "first")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Hello There")
.setContentText("General Master!")
.setStyle(
NotificationCompat.BigTextStyle()
.bigText(getString(R.string.big_string))
)
.setPriority(NotificationCompat.PRIORITY_DEFAULT).build()
//Create a simple notification, first - > Channel ID
val simpleNotification = NotificationCompat.Builder(this, "first")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Hello There")
.setContentText("General Master!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT).build()
//Show the notification, nm is the NotificationManager object
nm.notify(id, simpleNotification)
private fun createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = getString(R.string.channel_name) //name of the channel
val descriptionText = getString(R.string.channel_description)
val importance = NotificationManager.IMPORTANCE_DEFAULT //importance of the channel
// Create a NotificationChannel
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
description = descriptionText
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity">
import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.edit
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
val KEY_DATA = "data"
val KEY_APP_OPEN = "app_open"
var appOpenCount = 0
/*For scheduling a one time work request.
*/
button.setOnClickListener {
scheduleTask()
}
private fun scheduleRepeatingTasks() {
/*Setting up different constraints on the work request.
*/
val constraints = Constraints.Builder().apply {
setRequiredNetworkType(NetworkType.CONNECTED)
setRequiresCharging(true)
setRequiresStorageNotLow(true)
}.build()
private fun scheduleTask() {
// build an object of OneTimeWorkRequestBuilder
val workerRequest = OneTimeWorkRequestBuilder<NotificationRequestWorker>()
.setInitialDelay(20, TimeUnit.SECONDS)
.build()
// Enqueue the above workrequest object to the WorkManager
WorkManager.getInstance(this).enqueue(workerRequest)
}
// NotificationRequestWorker extends the worker class which defines the work to be performed by the worker.
class NotificationRequestWorker(val context: Context, workerParameters: WorkerParameters) :
Worker(context, workerParameters) {
override fun doWork(): Result {
// Do the work here
val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notification = NotificationCompat.Builder(context, "first").apply {
setContentTitle("Background task")
setContentText("Sample text")
setSmallIcon(R.drawable.ic_launcher_foreground)