. f Kotlin - Android notification in kotlin | Android MVP in Kotlin ~ Android Developers Blog

Monday, 11 May 2020

Kotlin - Android notification in kotlin | Android MVP in Kotlin

Hello Friends,
       Lets first tell you what is notification.


Notification is a message that is used to display some short messages outside of our main application. Even if the app is not running, notifications will still work. Notifications have the following contents: an icon, title of notification and some text content.

Now today I am going to share , how create notification in Kotlin.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/btn_send_notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_send_notification"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.developer.soulutions.notification">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".NotificationActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.android.developer.soulutions.notification
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.RemoteViews
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), MainActivityContract.View {
lateinit var notificationChannel: NotificationChannel
lateinit var notificationManager: NotificationManager
lateinit var builder: Notification.Builder
private lateinit var presenter: MainActivityContract.Presenter
private val channelId = "chanelId 1"
private val description = "Hi android devloper solutions,hi mukesh"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
MainAcivityPresenter(this)
initView()
}
private fun initView() {
btn_send_notification.setOnClickListener {
//it is a class to notify the user of events that happen.
// This is how you tell the user that something has happened in the
notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
presenter.onNotificationBtnClicked()
}
}
override fun setPresenter(presenter: MainActivityContract.Presenter) {
this.presenter = presenter
}
override fun createAndNotify() {
val intent = Intent(this,NotificationActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT)
//RemoteViews are used to use the content of
// some different layout apart from the current activity layout
val contentView = RemoteViews(packageName,R.layout.activity_notification)
//checking if android version is greater than oreo(API 26) or not
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = NotificationChannel(
channelId,description,NotificationManager.IMPORTANCE_HIGH)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.GREEN
notificationChannel.enableVibration(false)
notificationManager.createNotificationChannel(notificationChannel)
builder = Notification.Builder(this,channelId)
.setContentTitle("Notifications Example") //set title of notification
.setContentText("This is a notification message")//this is notification message
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(
BitmapFactory.decodeResource(this.resources,
R.drawable.ic_launcher_background))
.setContentIntent(pendingIntent)
}else{
builder = Notification.Builder(this)
.setContentTitle("Notifications Example") //set title of notification
.setContentText("This is a notification message")//this is notification message
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(this.resources,
R.drawable.ic_launcher_background))
.setContentIntent(pendingIntent)
}
notificationManager.notify(1234,builder.build())
}
}
view raw MainActivity.kt hosted with ❤ by GitHub
package com.android.developer.soulutions.notification
/**
* Created by Mukesh on 5/10/2020.
*/
interface MainActivityContract {
interface View {
fun setPresenter(presenter: Presenter)
fun createAndNotify()
}
interface Presenter {
fun start()
fun onNotificationBtnClicked()
}
}
package com.android.developer.soulutions.notification
/**
* Created by Mukesh on 5/10/2020.
* mukesh.yadav
*/
class MainActivityPresenter(private val view: MainActivityContract.View) : MainActivityContract.Presenter {
init {
start()
}
override fun start() {
view.setPresenter(this)
}
override fun onNotificationBtnClicked() {
view.createAndNotify()
}
}
Download code from here
Hope this will helps someone..
Enjoy Coding.... :)

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Blog.