নোটিফিকেশন থেকে নির্দিষ্ট অ্যাক্টিভিটি লঞ্চ করা (RelativeLayout UI)
এই উদাহরণটি ১০০৬০৬১৫৪৭ নম্বর আর্টিকেলের সমান, শুধু লেআউটে `ConstraintLayout`-এর বদলে `RelativeLayout` ব্যবহার করা হয়েছে। নোটিফিকেশনে `setContentIntent()` দিয়ে `PendingIntent` সেট করা হয়েছে।
ধাপ ১: প্রজেক্ট তৈরি
Android Studio-তে নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট (RelativeLayout)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/btnCreateNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:text="নোটিফিকেশন তৈরি করুন" />
</RelativeLayout>
ধাপ ৩: MainActivity.java
package com.example.notificationstartactivity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public static final String CHANNEL_ID = "start_activity_channel";
private static final String DEFAULT_CH = "default";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btnCreateNotification);
btn.setOnClickListener(v -> {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CH)
.setContentTitle("আমার নোটিফিকেশন")
.setContentText("Notification Listener Service Example")
.setTicker("Notification Listener Service Example")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent) // ★ ক্লিক করলে অ্যাক্টিভিটি খুলবে
.setAutoCancel(true);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel ch = new NotificationChannel(CHANNEL_ID, "Channel Name", NotificationManager.IMPORTANCE_HIGH);
builder.setChannelId(CHANNEL_ID);
if (nm != null) nm.createNotificationChannel(ch);
}
if (nm != null) nm.notify((int) System.currentTimeMillis(), builder.build());
});
}
}
ধাপ ৪: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.notificationstartactivity">
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.NotificationStartActivity">
<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>
</application>
</manifest>
পরীক্ষা
অ্যাপ রান করুন → বাটন চাপুন → নোটিফিকেশন ট্যাপ করলে MainActivity খুলবে।
