kotlin notification example

Android Notification gives short, convenient data about the activity occurred in the application, even it isn't running. The notification shows the symbol, title and some measure of the substance content.

Notification is a message that is utilized to show some short messages outside of our primary application. Regardless of whether the application isn't running, notifications will in any casework. Notifications have the accompanying substance: a symbol, title of notification and some content substance.

follow the example  kotlin notification example

Set Android Notification Properties

  • setSmallIcon(): it sets the icon of notification.
  • setContentTitle(): it is used to set the title of notification.
  • setContentText(): it is used to set the text message.
  • setAutoCancel(): it sets the cancelable property of notification.
  • setPriority(): it sets the priority of notification.

1.MainActivity.kt:

import android.app.NotificationManager;  
import android.app.PendingIntent;  
import android.content.Context;  
import android.content.Intent;  
import android.support.v4.app.NotificationCompat;  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
  
public class MainActivity extends AppCompatActivity {  
    Button button;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        button = findViewById(R.id.button);  
        button.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                addNotification();  
            }  
        });  
    }  
  
    private void addNotification() {  
        NotificationCompat.Builder builder =  
                new NotificationCompat.Builder(this)  
                        .setSmallIcon(R.drawable.messageicon) //set icon for notification  
                        .setContentTitle("Notifications Example") //set title of notification  
                        .setContentText("This is a notification message")//this is notification message  
                        .setAutoCancel(true) // makes auto cancel of notification  
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT); //set priority of notification  
  
  
        Intent notificationIntent = new Intent(this, NotificationView.class);  
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
        //notification message will get at NotificationView  
        notificationIntent.putExtra("message", "This is a notification message");  
  
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,  
                PendingIntent.FLAG_UPDATE_CURRENT);  
        builder.setContentIntent(pendingIntent);  
  
        // Add as notification  
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
        manager.notify(0, builder.build());  
    }  
}  



2.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">  
  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="ANDROID NOTIFICATION"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintLeft_toLeftOf="parent"  
        app:layout_constraintRight_toRightOf="parent"  
        app:layout_constraintTop_toTopOf="parent"  
        app:layout_constraintVertical_bias="0.091"  
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>  
  
    <Button  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:id="@+id/button"  
        android:layout_marginBottom="112dp"  
        android:layout_marginEnd="8dp"  
        android:layout_marginStart="8dp"  
        android:text="Notify"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintStart_toStartOf="parent" />  
  
</android.support.constraint.ConstraintLayout>  



3.NotificationView.kt:


import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.widget.TextView;  
import android.widget.Toast;  
  
public class NotificationView extends AppCompatActivity {  
    TextView textView;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_notification_view);  
        textView = findViewById(R.id.textView);  
        //getting the notification message  
        String message=getIntent().getStringExtra("message");  
        textView.setText(message);  
    }  
}  


4.activity_notification_view:


<?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=".NotificationView">  
  
    <TextView  
        android:id="@+id/textView2"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:gravity="center"  
        android:text="your detail of notification..."  
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />  
  
    <TextView  
        android:id="@+id/textView"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginBottom="8dp"  
        android:layout_marginEnd="8dp"  
        android:layout_marginStart="8dp"  
        android:layout_marginTop="8dp"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintHorizontal_bias="0.096"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toBottomOf="@+id/textView2"  
        app:layout_constraintVertical_bias="0.206"  
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>  
  
</android.support.constraint.ConstraintLayout>  



4.strings.xml:


<resources>  
    <string name="app_name">Kotlin Notification </string>  
    <string name="notification_activity">NotificationView</string>  
</resources>  



5.AndroidManifest.xml:


<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="example.akash.kotlinnotification">  
  
    <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=".MainActivity">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
        <activity android:name=".NotificationView"  
            android:label="@string/notification_activity"  
            android:parentActivityName=".MainActivity">  
            <meta-data  
                android:name="android.support.PARENT_ACTIVITY"  
                android:value=".MainActivity"/>  
        </activity>  
    </application>  
  
</manifest>  


Comments

Popular posts from this blog

Kotlin camera intent example

android kotlin current date example

android kotlin listview example