<?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]());
}
}