অ্যান্ড্রয়েডে নোটিফিকেশন কী?
নোটিফিকেশন হলো সিস্টেম tarafından একশন বারে দেখানো একটি বার্তা। যেমন— মিসড কল নোটিফিকেশন। নিচের ছবিতে একটি উদাহরণ দেখানো হয়েছে:

এই উদাহরণে দেখানো হয়েছে কিভাবে অ্যান্ড্রয়েড নোটিফিকেশন ইন্টিগ্রেট করতে হয়।
পদক্ষেপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project এ যান এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।
পদক্ষেপ ২: res/layout/activity_main.xml-এ কোড যোগ করুন
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
নোট: আধুনিক অ্যান্ড্রয়েড ডেভেলপমেন্টে AndroidX (androidx.constraintlayout.widget.ConstraintLayout) ব্যবহার করা উচিত।
পদক্ষেপ ৩: src/MainActivity.java-এ কোড যোগ করুন
package com.example.andy.myapplication;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(this);
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
notificationDialog();
break;
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void notificationDialog() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "tutorialspoint_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@SuppressLint("WrongConstant")
NotificationChannel notificationChannel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID,
"My Notifications",
NotificationManager.IMPORTANCE_MAX
);
// চ্যানেল কনফিগারেশন
notificationChannel.setDescription("Sample Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("Tutorialspoint")
//.setPriority(Notification.PRIORITY_MAX)
.setContentTitle("Sample Notification")
.setContentText("This is sample notification")
.setContentInfo("Information");
notificationManager.notify(1, notificationBuilder.build());
}
}
অ্যাপ্লিকেশন চালানোর পদ্ধতি
আপনার অ্যান্ড্রয়েড ডিভাইস কম্পিউটারের সাথে সংযুক্ত আছে বলে অনুমান করা হচ্ছে। Android Studio-তে প্রজেক্টের যেকোনো অ্যাক্টিভিটি ফাইল খুলুন এবং টুলবার থেকে Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস সিলেক্ট করুন এবং ডিভাইসে ডিফল্ট স্ক্রিন দেখুন।

এখন উপরের বাটনে ক্লিক করলে নিচের মতো আউটপুট দেখতে পাবেন:

মূল পয়েন্টসমূহ
- Android 8.0 (API 26) এরপর থেকে NotificationChannel তৈরি করা বাধ্যতামূলক।
NotificationCompat.Builderব্যবহার করে পুরনো ভার্সনেও কম্প্যাটিবল নোটিফিকেশন তৈরি করা যায়।setAutoCancel(true)সেট করলে নোটিফিকেশনে ক্লিক করলে সেটি অটোমেটিক্যালি মুছে যাবে।