0% found this document useful (0 votes)
10 views6 pages

Android Notification Setup Guide

The document contains an Android layout XML for a welcome message and a Java class for a MainActivity that handles notification creation. It includes methods to create a notification channel and send a notification with a custom layout when a button is clicked. The implementation ensures compatibility with Android versions and manages notification permissions appropriately.

Uploaded by

lordharish0123
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)
10 views6 pages

Android Notification Setup Guide

The document contains an Android layout XML for a welcome message and a Java class for a MainActivity that handles notification creation. It includes methods to create a notification channel and send a notification with a custom layout when a button is clicked. The implementation ensures compatibility with Android versions and manages notification permissions appropriately.

Uploaded by

lordharish0123
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

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

>
<LinearLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/white"
tools:context=".AfterNotification">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome To GeeksforGeeks"
android:textSize="15sp"
android:textStyle="bold" />

</LinearLayout>

MainActivity File:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private final String channelId = "[Link]"; // Unique


channel ID for notifications
private final String description = "Test notification"; // Description for
the notification channel
private final int notificationId = 1234; // Unique identifier for the
notification

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

Button btn = findViewById([Link]);

// Create a notification channel (required for Android 8.0 and


higher)
createNotificationChannel();

[Link](v -> {
// Request runtime permission for notifications on Android 13 and
higher
if ([Link].SDK_INT >=
Build.VERSION_CODES.TIRAMISU) {
if ([Link](this,
[Link].POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED) {

[Link](
this,
new String[]
{[Link].POST_NOTIFICATIONS},
101
);
return;
}
}
sendNotification(); // Trigger the notification
});
}

/**
* Create a notification channel for devices running Android 8.0 or
higher.
* A channel groups notifications with similar behavior.
*/
private void createNotificationChannel() {
if ([Link].SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new
NotificationChannel(
channelId,
description,
NotificationManager.IMPORTANCE_HIGH
);
[Link](true); // Turn on notification
light
[Link]([Link]);
[Link](true); // Allow vibration for
notifications

NotificationManager notificationManager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {

[Link](notificationChannel);
}
}
}

/**
* Build and send a notification with a custom layout and action.
*/
@SuppressLint("MissingPermission")
private void sendNotification() {
// Intent that triggers when the notification is tapped
Intent intent = new Intent(this, [Link]);
[Link](Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = [Link](
this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT |
PendingIntent.FLAG_IMMUTABLE
);

// Custom layout for the notification content


RemoteViews contentView = new RemoteViews(getPackageName(),
[Link].activity_after_notification);

// Build the notification


[Link] builder = new
[Link](this, channelId)
.setSmallIcon([Link].gfg_logo) // Notification icon
.setContent(contentView) // Custom notification content
.setContentTitle("Hello") // Title displayed in the notification
.setContentText("Welcome to GeeksforGeeks!!") // Text
displayed in the notification
.setContentIntent(pendingIntent) // Pending intent triggered
when tapped
.setAutoCancel(true) // Dismiss notification when tapped
.setPriority(NotificationCompat.PRIORITY_HIGH); //
Notification priority for better visibility

// Display the notification


NotificationManagerCompat notificationManager =
[Link](this);
[Link](notificationId, [Link]());
}
}

You might also like