Experiment - 03-MC

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

NAME : KRUNAL PATEL ROLL NO: 17 CLASS: TE_CMPN-

B1

EXPERIMENT - 03

Aim : Implement an application that creates an alert upon receiving a message.


Theory:
1) Explain Notification creation and its attributes
In Android, a Notification is a message that appears outside of the app's UI to provide the user
with some information or to alert them about an event. Notifications can be created using the
NotificationCompat.Builder class, which provides a set of attributes to customize the
notification's appearance and behavior.
Here are some of the common attributes that can be set when creating a Notification:
● Notification title: The title of the notification, which appears at the top of the notification
message. This can be set using the setContentTitle() method.
● Notification message: The message or content of the notification, which appears below
the title. This can be set using the setContentText() method.
● Notification icon: The icon that appears on the left side of the notification message. This
can be set using the setSmallIcon() method.
● Notification sound: The sound that plays when the notification is received. This can be
set using the setSound() method.
● Notification vibration: The vibration pattern that plays when the notification is received.
This can be set using the setVibration() method.
● Notification action: The action that should be performed when the user taps on the
notification. This can be set using the addAction() method.
● Notification priority:The priority level of the notification, which determines how
prominently it is displayed. This can be set using the setPriority() method.

2) Explain Manger creation and its attributes


The NotificationManager is responsible for handling notifications and managing their display on
the user's device. It has several attributes that can be set to customize the behavior of
notifications, including:
● Importance: This attribute determines the level of priority that the notification should
have. Notifications can be set to one of five levels of importance: MIN, LOW, DEFAULT,
HIGH, or MAX. Notifications with higher importance levels are more likely to be displayed
to the user, and may trigger additional user interaction such as sound or vibration.
● Channel ID: This attribute is used to group notifications by category, such as "messages"
or "alerts." Each notification must be assigned to a specific channel ID, and the attributes of
that channel (such as sound or vibration settings) will apply to all notifications in that
channel.
● Content Intent: This attribute specifies the action that should be taken when the user
clicks on the notification. Typically, this is used to launch an activity in the app or display
additional information about the notification.
● Small Icon: This attribute is used to specify the icon that should be displayed with the
notification in the status bar. The icon should be a small, grayscale version of the app's main
icon.
NAME : KRUNAL PATEL ROLL NO: 17 CLASS: TE_CMPN-
B1

● Notification Text: This attribute sets the main text that should be displayed with the
notification. This can include a short message or summary of the notification content.
3) Explain Channel creation and its attributes
In Android Studio, creating a channel for notification alerts refers to the process of setting up a
channel for delivering notifications to the user. Here are some common attributes of a
notification channel in Android Studio:
● ID: The ID of a notification channel is a unique identifier for the channel that is used to
identify and manage the channel within the app.
● Name: The name of a notification channel is the human-readable name of the channel
that is displayed to the user in the device's settings.
● Description: The description of a notification channel is a brief summary of the type of
notifications that will be delivered through the channel.
● Importance: The importance of a notification channel refers to the level of priority that
the notifications delivered through the channel will have. Android Studio provides four levels
of importance: High, Default, Low, and Min.
● Sound: The sound of a notification channel refers to the sound that will be played when a
notification is delivered through the channel.
● Vibration: The vibration of a notification channel refers to the vibration pattern that will
be used when a notification is delivered through the channel.
● LED Light: The LED light of a notification channel refers to the color and pattern of the
LED light that will be used when a notification is delivered through the channel.

Program Code:
MainActivity.java setContentView(R.layout.activity_main);
package com.example.message_notify_b1_17; Button
notifyButton=findViewById(R.id.button_notif
import android.app.NotificationChannel; y);
import android.app.NotificationManager; EditText
import android.os.Build; messageText=findViewById(R.id.my_messag
import android.os.Bundle; e);
import notifyButton.setOnClickListener(new
android.support.v4.app.NotificationCompat; View.OnClickListener() {
import @Override
android.support.v4.app.NotificationManagerC public void onClick(View v) {
ompat; String
import message=messageText.getText().toString();
android.support.v7.app.AppCompatActivity; NotificationCompat.Builder
import android.view.View; builder=new
import android.widget.Button; NotificationCompat.Builder(MainActivity.this
import android.widget.EditText; ,"My Notification")
public class MainActivity extends .setContentTitle("Alert")
AppCompatActivity { .setContentText(message)
@Override .setSmallIcon(R.drawable.baseline_notificatio
protected void onCreate(Bundle ns_active_24);
savedInstanceState) { NotificationManagerCompat
super.onCreate(savedInstanceState); managerCompat=
NAME : KRUNAL PATEL ROLL NO: 17 CLASS: TE_CMPN-
B1

NotificationManagerCompat.from(MainActivi
ty.this); android:src="@drawable/baseline_arrow_back_
managerCompat.notify(Integer.parseInt("1"), 24" />
builder.build()); <ImageView
if(Build.VERSION.SDK_INT>=Build.VERSI android:layout_width="wrap_content"
ON_CODES.O){ android:layout_height="wrap_content"
NotificationChannel channel=new
NotificationChannel("My android:src="@drawable/baseline_call_24" />
Notification","Notify",NotificationManager.I
MPORTANCE_DEFAULT); <ImageView
NotificationManager android:layout_width="wrap_content"
manager=getSystemService(NotificationMana android:layout_height="wrap_content"
ger.class);
android:src="@drawable/baseline_search_24" /
manager.createNotificationChannel(channel); >
} </LinearLayout>
} <LinearLayout
}); android:layout_width="wrap_content"
}} android:layout_height="wrap_content"
android:layout_gravity="center"
Activity_main.xml android:layout_marginTop="10dp">
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout <TextView
xmlns:android="http://schemas.android.com/apk android:layout_width="wrap_content"
/res/android" android:layout_height="wrap_content"
android:text="To"
xmlns:tools="http://schemas.android.com/tools" android:textColor="@color/black"
android:layout_width="match_parent" android:layout_marginRight="25px"
android:layout_height="match_parent" android:textSize="25dp" />
android:orientation="vertical">
<ImageView
<TextView android:layout_width="wrap_content"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_height="wrap_content" android:layout_marginLeft="330dp"
android:layout_marginLeft="80dp"
android:text="New Message" android:src="@drawable/baseline_person_add_
android:textSize="40dp" 24" />
android:layout_marginTop="10dp" </LinearLayout>
android:textColor="@color/black"/>
<LinearLayout
<LinearLayout
android:layout_width="wrap_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_gravity="center"
android:layout_marginTop="500dp">
android:layout_marginBottom="0dp">
<ImageView
<ImageView
android:layout_width="wrap_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginRight="325dp"
NAME : KRUNAL PATEL ROLL NO: 17 CLASS: TE_CMPN-
B1

android:layout_height="wrap_content"
android:src="@drawable/baseline_add_circle_o android:layout_margin="10dp"
utline_24" />
<EditText android:src="@drawable/baseline_emoji_emoti
android:id="@+id/my_message" ons_24" />
android:layout_width="wrap_content"
android:layout_height="wrap_content" <Button
android:hint="Type a Message" android:id="@+id/button_notify"
android:textAlignment="center" android:layout_width="wrap_content"
android:textSize="20dp" android:layout_height="wrap_content"
android:width="160dp" android:background="@color/black"
android:text="Send"
tools:ignore="TouchTargetSizeCheck" /> android:textColor="@color/white" />
</LinearLayout>
<ImageView
</LinearLayout>
android:layout_width="wrap_content"

OUTPUT:

Conclusion : Thus we have successfully created a notification alert application that gives an alert message in
the notification panel . In the MainActivity.java We have used the NotificationCompat.Builder class of java that
provides attributes and customizations to the notification alert also we have used the Notification Manager.class
NAME : KRUNAL PATEL ROLL NO: 17 CLASS: TE_CMPN-
B1

for setting the Importance of the notification alert i.e. whether the given notification alert needs an importance of
HIGH MEDIUM or LOW . While using Activity_main.xml has been used to give the basic layout to our
notification application and also set up the notification icon for the alert we are getting in the notification panel.
We have understood the working of these classes and how to implement them in different ways in our application.

You might also like