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

অ্যান্ড্রয়েডে Firebase Messaging ব্যবহারের পদ্ধতি

Firebase Cloud Messaging (FCM) বাস্তবায়ন করা

এই উদাহরণটি দেখাবে কীভাবে FirebaseMessagingService এক্সটেন্ড করে পুশ নোটিফিকেশন হ্যান্ডল করা যায়।

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

Android Studio-তে File → New Project নির্বাচন করুন।

ধাপ ২: মেইন এক্টিভিটি (src/MainActivity.java)

package com.example.myapplication;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

ধাপ ৩: ফায়ারবেস মেসেজিং সার্ভিস (src/MyFirebaseMessagingService.java)

package com.example.myapplication;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import org.json.JSONObject;
import java.util.Map;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onNewToken(String s) {
        Log.e("NEW_TOKEN", s);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Map<String, String> params = remoteMessage.getData();
        JSONObject object = new JSONObject(params);
        Log.e("JSON_OBJECT", object.toString());
        String NOTIFICATION_CHANNEL_ID = "sairam";
        long pattern[] = {0, 1000, 500, 1000};
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications", NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.setDescription("");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(pattern);
            notificationChannel.enableVibration(true);
            mNotificationManager.createNotificationChannel(notificationChannel);
        }
        // DND মোডে নোটিফিকেশন দেখানোর জন্য
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
            channel.canBypassDnd();
        }
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        notificationBuilder.setAutoCancel(true)
                .setColor(ContextCompat.getColor(this, R.color.colorAccent))
                .setContentTitle(getString(R.string.app_name))
                .setContentText(remoteMessage.getNotification().getBody())
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setAutoCancel(true);
        mNotificationManager.notify(1000, notificationBuilder.build());
    }
}

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

অ্যাপ চালু করুন। Firebase Console থেকে টেস্ট নোটিফিকেশন পাঠালে ডিভাইসে দেখাবে।

অ্যান্ড্রয়েডে Firebase Messaging ব্যবহারের পদ্ধতি

  1. অ্যান্ড্রয়েড ফ্র্যাগমেন্টে Context ব্যবহারের পদ্ধতি

  2. অ্যান্ড্রয়েডে Firebase Messaging ব্যবহারের পদ্ধতি

  3. অ্যান্ড্রয়েড টুলবারে SearchView ব্যবহার করার পদ্ধতি

  4. অ্যান্ড্রয়েডে XMLPullParser ব্যবহার করে XML পার্স করার পদ্ধতি