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

Rushimc 3 Rdexample

The document provides instructions for creating a Notification Activity using Android, including XML layout files and Java code. It outlines the setup for notification permissions, the creation of a notification channel, and the display of notifications. Additionally, it includes sample code for handling button clicks to trigger notifications and navigate to another activity.

Uploaded by

urbanwatch7
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)
6 views5 pages

Rushimc 3 Rdexample

The document provides instructions for creating a Notification Activity using Android, including XML layout files and Java code. It outlines the setup for notification permissions, the creation of a notification channel, and the display of notifications. Additionally, it includes sample code for handling button clicks to trigger notifications and navigate to another activity.

Uploaded by

urbanwatch7
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

MobileComputing(09CE1501)

Examples-3

Aim:-Usingandroid,CreateaNotificationActivity.

Notification.xml

<?xmlversion="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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"android:text="Sen
d
Messages"app:layout_constraintBottom_toBottomOf="p
arent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"tools:ignore
="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

Notification.java

packagecom.example.ex_4;

import android.Manifest;
importandroid.app.Activity;
import android.app.NotificationChannel;
importandroid.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
importandroid.content.Intent;
importandroid.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
importandroid.widget.Button;

importandroidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
importandroidx.core.app.NotificationCompat;
importandroidx.core.content.ContextCompat;

publicclassMainActivityextendsAppCompatActivity{

Neel Teliyani 1 92200938045


MobileComputing(09CE1501)
privatestaticfinalintREQUEST_NOTIFICATION_PERMISSION=1;

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//CheckforNotificationPermissiononAndroid13(API33)orabove if
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.POST_NOTIFICATIONS)
==PackageManager.PERMISSION_DENIED){
// Request permission if not granted
ActivityCompat.requestPermissions(this,new
String[]{Manifest.permission.POST_NOTIFICATIONS},
REQUEST_NOTIFICATION_PERMISSION);
}
}

Button b1 = findViewById(R.id.button);
b1.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
showNotification();
}
});
}

//Methodtoshowthenotification
public void showNotification() {
Stringchannel_id="RRM_channel";
NotificationCompat.Builderbuilder=newNotificationCompat.Builder(this,channel_id)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Ramij'sNotification")
.setContentText("Hello,Howareyou?")
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);

//IntenttoopenMainActivity2
Intentintent=new Intent(this,MainActivity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("data", "Hello, How are You?");

//PendingIntenttoopenMainActivity2whenthenotificationisclicked PendingIntent
pendingIntent = PendingIntent.getActivity(this,
0,intent,PendingIntent.FLAG_MUTABLE);

builder.setContentIntent(pendingIntent);

//GetNotificationManagersystemservice
NotificationManagernotificationManager=(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
//CreatethenotificationchannelonAndroidO(APIlevel26)orabove NotificationChannel
notificationChannel =
notificationManager.getNotificationChannel(channel_id);

Neel Teliyani 2 92200938045


MobileComputing(09CE1501)
if(notificationChannel==null){
int importance = NotificationManager.IMPORTANCE_HIGH;
notificationChannel=newNotificationChannel(channel_id,"RRM_channel",
importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.GREEN);
notificationManager.createNotificationChannel(notificationChannel);
}
}

// Show the notification


notificationManager.notify(0,builder.build());
}
}

Home.xml

<?xmlversion="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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Home">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"android:text="W
elcome
Ramij"app:layout_constraintBottom_toBottomOf="pare
nt" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"app:layo
ut_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Neel Teliyani 3 92200938045


MobileComputing(09CE1501)

OUTPUT

Neel Teliyani 4 92200938045


MobileComputing(09CE1501)

Neel Teliyani 5 92200938045

You might also like