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.
Download code from here
Hope this will helps someone..
Enjoy Coding.... :)
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
Hope this will helps someone..
Enjoy Coding.... :)
0 comments:
Post a Comment