PendingIntent দিয়ে নোটিফিকেশন থেকে অ্যাক্টিভিটি লঞ্চ করা
এই উদাহরণটি দেখায় কীভাবে একটি নোটিফিকেশনের setContentIntent() মেথডে PendingIntent পাস করে ব্যবহারকারী নোটিফিকেশনটিতে ট্যাপ করলে নির্দিষ্ট অ্যাক্টিভিটি খোলা যায়।
ধাপ ১: প্রজেক্ট তৈরি
Android Studio-তে নতুন একটি Empty Views Activity প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট ফাইল
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
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="0dp"
android:layout_height="wrap_content"
android:text="নোটিফিকেশন দেখান"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
ধাপ ৩: MainActivity.java - কন্টেন্ট ইচ্ছা সেট করা
খেয়াল করুন PendingIntent.getActivity() এবং builder.setContentIntent() ব্যবহার:
package com.example.notificationclick;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
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 = "notify_channel_01";
private static final String DEFAULT_CHANNEL = "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);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CHANNEL)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("নোটিফিকেশন টাইটেল")
.setContentText("এখানে ট্যাপ করলে MainActivity খুলবে")
.setContentIntent(pendingIntent) // ★ ক্লিক হ্যান্ডলার সেট
.setAutoCancel(true) // ক্লিক করলে মুছে যাবে
.setStyle(new NotificationCompat.InboxStyle()
.addLine("লাইন ১")
.addLine("লাইন ২")
.addLine("লাইন ৩"));
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID, "নোটিফিকেশন চ্যানেল", NotificationManager.IMPORTANCE_HIGH);
builder.setChannelId(CHANNEL_ID);
if (manager != null) manager.createNotificationChannel(channel);
}
int notifId = (int) System.currentTimeMillis();
if (manager != null) manager.notify(notifId, builder.build());
});
}
}
ধাপ ৪: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.notificationclick">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.NotificationClick">
<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 পুনরায় খুলবে (বা আগের ইন্সট্যান্সটি ফোরগ্রাউন্ডে আসবে) এবং নোটিফিকেশনটি অটোমেটিক মুছে যাবে।
