0% found this document useful (0 votes)
67 views5 pages

Practical 8 B

The document discusses Android notifications and provides an example code to create a notification that launches another activity when clicked. The example code sets notification properties like icon, title and text using NotificationCompat.Builder. It also creates a notification channel for Android O and above. When the notification is clicked, it passes the message to the launched activity's textview.

Uploaded by

jenni koko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views5 pages

Practical 8 B

The document discusses Android notifications and provides an example code to create a notification that launches another activity when clicked. The example code sets notification properties like icon, title and text using NotificationCompat.Builder. It also creates a notification channel for Android O and above. When the notification is clicked, it passes the message to the launched activity's textview.

Uploaded by

jenni koko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: suyash munde Roll no.

47

PRACTICAL 8

B. Programs on Services, notification and broadcast receivers


Android Notification
Android Notification provides short, timely information about the action happened in the
application, even it is not running. The notification displays the icon, title and some amount
of the content text.
Set Android Notification Properties
The properties of Android notification are set using NotificationCompat.Builder object.
Some of the notification properties are mention below:

 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.

Android Notification Example


In this example, we will create a notification message which will launch another activity after
clicking on it.
activity_main.xml
Add the following code in an activity_main.xml file.

<?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">

<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"

1
Name: suyash munde Roll no.47

app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Create an activity named as activity_notification_view.xml and add the following code.


This activity will be launched on clicking the notification. TextView is used to display
the notification message.

activity_notification_view.xml
<?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=".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"
tools:ignore="MissingConstraints" />

<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"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.practical8b;

import androidx.appcompat.app.AppCompatActivity;

import android.app.NotificationChannel;
import android.app.NotificationManager;

2
Name: suyash munde Roll no.47

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.os.Build;
import android.view.View;
import android.widget.Button;

import android.os.Bundle;

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);

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {


NotificationChannel channel = new NotificationChannel("My
Notification", "My Notification", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager =
getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

NotificationCompat.Builder builder = new


NotificationCompat.Builder(MainActivity.this,"My Notification");
builder.setContentTitle("Notification");
builder.setContentText("This is a notification message");
builder.setSmallIcon(R.drawable.messageicon);
builder.setAutoCancel(true);

NotificationManagerCompat managerCompat =
NotificationManagerCompat.from(MainActivity.this);
managerCompat.notify(1,builder.build());

}
});
}
}

NotificationView.java
package com.practical8b;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class NotificationView extends AppCompatActivity {

3
Name: suyash munde Roll no.47

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);
}
}

strings.xml
<resources>
<string name="app_name">AndroidNotification</string>
<string name="notification_activity">NotificationView</string>
</resources>

AndroidManifest.xml
Add the following code in AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"
/>
<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/Theme.Practical8b">
<activity android:name=".MainActivity"
android:exported="true">
<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>

4
Name: suyash munde Roll no.47

Output:

NOTIFICATION ICON

NOTIFICATION
MESSAGE

You might also like