Create a new Project in Kotlin
Message can display outside of our application normal UI
1 Open Android Studio.
2 Go to File => New => New Project. Write application name . Then, check Include Kotlin Support and click next button.
3Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4 Then, select Empty Activity => click next => click finish.
5 You will get a newly created project successfully if you have followed steps properly.
added line in the build.gradle (Module: app) dependencies:
implementation 'com.android.support:appcompat-v7:26.1.0'
Use the below code to Implement a Notification with Kotlin
MainActivity.k
import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.* import kotlinx.android.synthetic.main.activity_main.* import android.widget.RadioGroup import android.widget.Toast import android.app.NotificationManager import android.support.v4.app.NotificationCompat import android.os.Build import android.app.NotificationChannel import android.content.Context import android.support.annotation.RequiresApi class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val button:Button=findViewById(R.id.button); button.setOnClickListener() { issueNotification() } } @RequiresApi(api = Build.VERSION_CODES.O) fun makeNotificationChannel(id: String, name: String, importance: Int) { val channel = NotificationChannel(id, name, importance) channel.setShowBadge(true) // set false to disable badges, Oreo exclusive val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.createNotificationChannel(channel) } fun issueNotification() { // make the channel. The method has been discussed before. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { makeNotificationChannel("CHANNEL_1", "Example channel", NotificationManager.IMPORTANCE_DEFAULT) } // the check ensures that the channel will only be made // if the device is running Android 8+ val notification = NotificationCompat.Builder(this, "CHANNEL_1") // the second parameter is the channel id. // it should be the same as passed to the makeNotificationChannel() method notification .setSmallIcon(R.mipmap.ic_launcher) // can use any other icon .setContentTitle("Notification!") .setContentText("This is an Oreo notification!") .setNumber(3) // this shows a number in the notification dots val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.notify(1, notification.build()) // it is better to not use 0 as notification id, so used 1. } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/root_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/button" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:text="click" /> </LinearLayout>

