0% found this document useful (0 votes)
8 views2 pages

Notification

The document contains an Android application code with a layout defined in XML and a Kotlin activity class. The layout includes a button that, when clicked, triggers the creation and sending of a notification. The notification is set up with a channel for devices running Android Oreo and above.

Uploaded by

sahilgolatkar0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Notification

The document contains an Android application code with a layout defined in XML and a Kotlin activity class. The layout includes a button that, when clicked, triggers the creation and sending of a notification. The notification is set up with a channel for devices running Android Oreo and above.

Uploaded by

sahilgolatkar0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Code:

Activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/btnnotify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLICK ME!!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

MainActivity.kt :
package com.example.myapplication

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.v4.app.NotificationCompat
import android.support.v4.app.NotificationManagerCompat
import android.widget.Button
class MainActivity : AppCompatActivity() {
private val channelId = "sample"
private val notifyId = 101
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn1 = findViewById<Button>(R.id.btnnotify)
btn1.setOnClickListener(){
createNotification()
sendNotificationChannel()
}
}
@RequiresApi(Build.VERSION_CODES.O)
private fun createNotification(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val name = "Sample Channel"
val desText = "This is a notification channel"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(channelId,name,importance).apply
{description =desText}
val notifyManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notifyManager.createNotificationChannel(channel)
}
}
private fun sendNotificationChannel(){
val builder = NotificationCompat.Builder(this,channelId)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Notify")
.setContentText("I'm sending notificaton....")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)

with(NotificationManagerCompat.from(this)){
notify(notifyId,builder.build())
}
}
}

You might also like