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

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

Notification Badge / Badge Count বাস্তবায়ন

এই টিউটোরিয়ালটি দেখায় কীভাবে অ্যাপ আইকনে নোটিফিকেশন ব্যাজ (সংখ্যা) দেখানো যায় NotificationCompat.Builder এবং setNumber() ব্যবহার করে।

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

Android Studio-তে নতুন প্রজেক্ট।

ধাপ ২: লেআউট (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:onClick="createNotification"
        android:text="নোটিফিকেশন তৈরি করুন" />
        
</RelativeLayout>

ধাপ ৩: MainActivity.java

package com.app.sample;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.annotation.SuppressLint;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private static int count = 0;
    private static final String CHANNEL_ID = "notification_count_channel";
    private static final String CHANNEL_NAME = "Notification Counter";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        createNotificationChannel();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // অ্যাপ খুললে কাউন্ট রিসেট (ঐচ্ছিক)
        count = 0;
        updateBadge(0);
    }

    @SuppressLint("WrongConstant")
    public void createNotification(View view) {
        count++;
        
        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.putExtra("fromNotification", true);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        
        PendingIntent pendingIntent = PendingIntent.getActivity(
            this, 0, notificationIntent, 
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_IMMUTABLE : 0
        );
        
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("আমার নোটিফিকেশন")
            .setContentText("নোটিফিকেশন লিসেনার সার্ভিস উদাহরণ")
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .setNumber(count)  // <-- ব্যাজ কাউন্ট সেট করুন
            .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
            .setPriority(NotificationCompat.PRIORITY_HIGH);
        
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify((int) System.currentTimeMillis(), builder.build());
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            channel.setDescription("নোটিফিকেশন কাউন্ট ডেমো চ্যানেল");
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }
    }
    
    // প্রোগ্রামেটিক্যালি ব্যাজ আপডেট (নোটিফিকেশন ছাড়া)
    private void updateBadge(int count) {
        // কিছু লঞ্চার শুধু নোটিফিকেশন থেকে ব্যাজ পড়ে
        // একটি "ফেক" নোটিফিকেশন পোস্ট করুন শুধু ব্যাজ আপডেটের জন্য
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("")
            .setContentText("")
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setNumber(count)
            .setPriority(NotificationCompat.PRIORITY_MIN)
            .setCategory(NotificationCompat.CATEGORY_SERVICE);
        
        NotificationManagerCompat.from(this).notify(9999, builder.build());
    }
}

ধাপ ৪: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.app.sample">
    
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <!-- Android 13+ নোটিফিকেশন পারমিশন -->
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    
    <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>

গুরুত্বপূর্ণ নোটস ব্যাজ কাউন্ট সম্পর্কে

  • লঞ্চার সাপোর্ট: সব লঞ্চার ব্যাজ সাপোর্ট করে না (Pixel Launcher, Nova, OnePlus, Samsung সাপোর্ট করে)
  • Notification Channel: Android 8.0+ (API 26+) এ চ্যানেল বাধ্যতামূলক
  • setNumber(): এই মেথডটি ব্যাজ কাউন্ট সেট করে
  • Auto-cancel: ব্যবহারকারী নোটিফিকেশন ট্যাপ করলে ব্যাজ কমে যাবে না (ম্যানুয়ালি হ্যান্ডল করুন)

উন্নত: ShortcutBadger লাইব্রেরি (ব্রড লঞ্চার সাপোর্ট)

// build.gradle
implementation "me.leolin:ShortcutBadger:1.1.22@aar"

// ব্যবহার
ShortcutBadger.applyCount(context, count); // কোনো লঞ্চার সাপোর্ট করে
ShortcutBadger.removeCount(context); // ব্যাজ মুছুন

Android 12+ (API 31+) এ Notification Permission

// রানটাইমে পারমিশন চেক
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) 
        != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, 1001);
    }
}

ব্যাজ ক্লিয়ার করা (অ্যাপ খোলার সময়)

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    count = 0;
    // সকল নোটিফিকেশন ক্যান্সেল
    NotificationManagerCompat.from(this).cancelAll();
    // বা নির্দিষ্ট আইডি
    // notificationManager.cancel(NOTIFICATION_ID);
}
  1. অ্যান্ড্রয়েডে Toast মেসেজ দেখানোর পদ্ধতি

  2. অ্যান্ড্রয়েড অ্যাপে TextToSpeech তৈরি করার পদ্ধতি

  3. Facebook-এ Android অ্যাপ তৈরি করার পদ্ধতি

  4. এন্ড্রয়েড ৮ Snooze মাস্টার করুন: ভালো ঘুমের জন্য নোটিফিকেশন ম্যানেজ করুন