কম্পিউটার টিউটোরিয়াল

কোটলিন ব্যবহার করে অ্যান্ড্রয়েড অ্যাপ লঞ্চারে নোটিফিকেশন কাউন্ট দেখানোর পদ্ধতি

এই উদাহরণটি দেখায় কিভাবে কোটলিন ব্যবহার করে অ্যান্ড্রয়েড অ্যাপ লঞ্চার আইকনে নোটিফিকেশন বেজ (Notification Badge/Count) দেখানো যায়। NotificationCompat.Builder-র setNumber() মেথড ব্যবহার করা হয়েছে।

প্রথম ধাপ: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project এ যান এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।

দ্বিতীয় ধাপ: লেআউট XML ডিজাইন করুন

res/layout/activity_main.xml ফাইলে নিচের কোড যোগ করুন:

<?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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Tutorials Point"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="32sp"
        android:textStyle="bold" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:onClick="createNotification"
        android:text="নোটিফিকেশন তৈরি করুন" />

</RelativeLayout>

তৃতীয় ধাপ: MainActivity.kt তে নোটিফিকেশন লজিক লিখুন

src/MainActivity.kt ফাইলে নিচের কোড যোগ করুন। মূল কোডে ডিফল্ট চ্যানেল আইডি এবং কাস্টম চ্যানেল আইডি মিক্সড ছিল, সেটি সংশোধন করা হয়েছে:

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat

class MainActivity : AppCompatActivity() {
    private var count = 0
    private val channelId = "notification_channel_001"
    private val channelName = "নোটিফিকেশন চ্যানেল"

    override fun onResume() {
        super.onResume()
        // অ্যাপ ফোরগ্রাউন্ডে এলে বেজ কাউন্ট রিসেট করুন (ঐচ্ছিক)
        count = 0
        updateBadge(0)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        createNotificationChannel()
    }

    fun createNotification(view: View) {
        count++

        val notificationIntent = Intent(applicationContext, MainActivity::class.java)
        notificationIntent.putExtra("fromNotification", true)
        notificationIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP

        val pendingIntent = PendingIntent.getActivity(
            this, 0, notificationIntent,
            PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
        )

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        val builder = NotificationCompat.Builder(this, channelId)
            .setContentTitle("আমার নোটিফিকেশন")
            .setContentIntent(pendingIntent)
            .setContentText("নোটিফিকেশন লিসেনার সার্ভিস উদাহরণ")
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setAutoCancel(true)
            .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
            .setNumber(count) // লঞ্চার বেজ কাউন্ট সেট করুন

        notificationManager.notify(System.currentTimeMillis().toInt(), builder.build())
    }

    private fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val importance = NotificationManager.IMPORTANCE_HIGH
            val notificationChannel = NotificationChannel(channelId, channelName, importance)
            val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(notificationChannel)
        }
    }

    // বেজ কাউন্ট ম্যানুয়ালি আপডেট করার হেল্পার (ঐচ্ছিক)
    private fun updateBadge(count: Int) {
        // কিছু লঞ্চার শর্টকাট বেজ API সাপোর্ট করে (যেমন Sony, Samsung, Nova Launcher)
        // স্ট্যান্ডার্ড এপিআই নেই, তাই setNumber() হলো সবচেয়ে নির্ভরযোগ্য পদ্ধতি
    }
}

চতুর্থ ধাপ: ম্যানিফেস্ট ফাইল

androidManifest.xml ফাইলে VIBRATE পারমিশন যোগ করুন (নোটিফিকেশন ভাইব্রেশনের জন্য):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.q11">

    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

গুরুত্বপূর্ণ নোট

  • লঞ্চার সাপোর্ট: সব লঞ্চার বেজ কাউন্ট সাপোর্ট করে না। Google Pixel Launcher, Nova Launcher, Samsung One UI, Sony Launcher ইত্যাদি সাপোর্ট করে।
  • Android 8.0+: নোটিফিকেশন চ্যানেল বাধ্যতামূলক।
  • setNumber(): এটি নোটিফিকেশনের বেজ কাউন্ট সেট করে। লঞ্চার এই মান ব্যবহার করে আইকনে বেজ দেখায়।
  • PendingIntent.FLAG_IMMUTABLE: Android 12+ (API 31+) এ বাধ্যতামূলক।
  • NotificationListenerService: সিস্টেম-লেভেল নোটিফিকেশন মনিটরিং এবং বেজ কাউন্ট সিঙ্ক করার জন্য এটি ব্যবহার করা হয় (অ্যাডভান্সড)।

অ্যাপ্লিকেশন চালানোর নির্দেশনা

অ্যান্ড্রয়েড ডিভাইস সংযুক্ত করে Run করুন। "নোটিফিকেশন তৈরি করুন" বাটনে একাধিকবার ক্লিক করুন। প্রতিবার ক্লিক করলে নোটিফিকেশন আসবে এবং লঞ্চার আইকনে বেজ কাউন্ট বাড়বে। অ্যাপ খোললে (onResume) কাউন্ট রিসেট হবে।

  1. টুলবার আইকনে নোটিফিকেশন কাউন্ট বッジ দেখানো

  2. Android অ্যাপ লঞ্চার আইকনে নোটিফিকেশন কাউন্ট দেখানোর পদ্ধতি

  3. অ্যান্ড্রয়েডে Toast মেসেজ দেখানোর পদ্ধতি

  4. অ্যান্ড্রয়েড অ্যাপে নোটিফিকেশন কাউন্ট কীভাবে দেখাবেন?