Monday, January 15, 2018

Send Notification In Android Using Kotlin

Video Demo: 

Notification is used to notify the user regarding the events happening in the application without open it. To generate the notification in android we used NotificationBuilder  to generate the notification. PendingIntent  is used to mange the click on notification tray. So In tutorial I am showing to How to generate notification in android using kotlin.

Send Notification In Android  Using Kotlin


activity_main:
>RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    xmlns:android="http://schemas.android.com/apk/res/android">

    >TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="http://deepshikhapuri.blogspot.in/"
        android:textSize="18dp"
        android:id="@+id/tv_website"
        android:textColor="#0000FF"
        android:layout_marginTop="25dp"
        android:layout_centerHorizontal="true"/>

    >TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Notification Tutorial"
        android:textSize="20dp"
        android:textStyle="bold"
        android:layout_below="@+id/tv_website"
        android:textColor="#000000"
        android:layout_marginTop="25dp"
        android:layout_centerHorizontal="true"/>

    >ImageView
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_centerInParent="true"
        android:src="@drawable/android"/>
    >Button
        android:layout_width="match_parent"
        android:id="@+id/btn_notify"
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content"
        android:text="Send Notification"/>
>/RelativeLayout>

MainActivity.kt:
package com.deepshikha.notification

import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.NotificationCompat
import android.view.View
import android.widget.Button
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import android.media.RingtoneManager
import android.text.method.LinkMovementMethod
import android.text.util.Linkify


class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        Linkify.addLinks(tv_website, Linkify.ALL);
        btn_notify.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                val intent = Intent(applicationContext, MainActivity::class.java)
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                val pendingIntent = PendingIntent.getActivity(applicationContext, 0, intent, PendingIntent.FLAG_ONE_SHOT)
                val uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

                val notificationBuilder = NotificationCompat.Builder(applicationContext)
                        .setContentText("Dummy Notification")
                        .setAutoCancel(true)
                        .setSound(uri)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentIntent(pendingIntent)
                val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
            }
        })
    }
}

No comments:

Post a Comment