| প্ল্যাটফর্ম বেছে নিন: | iOS+ Android ওয়েব Flutter Unity C++ |
পৃষ্ঠাটি ফোরগ্রাউন্ডে (ফোকাস করা আছে) আছে নাকি ব্যাকগ্রাউন্ডে, অন্য ট্যাবের
আড়ালে লুকানো আছে নাকি সম্পূর্ণ বন্ধ করা আছে, তার উপর নির্ভর করে মেসেজের আচরণ আলাদা আলাদা হয়। সব ক্ষেত্রেই পৃষ্ঠাটিকে অবশ্যই
onMessage
কলব্যাক ম্যানেজ করতে হবে, তবে ব্যাকগ্রাউন্ডের ক্ষেত্রে আপনাকে
onBackgroundMessage
ম্যানেজ করতে হতে পারে
অথবা ব্যবহারকারীকে আপনার ওয়েব অ্যাপ ফোরগ্রাউন্ডে আনার অনুমতি দিতে ডিসপ্লে বিজ্ঞপ্তি কনফিগার করতে হতে পারে।
| অ্যাপের স্ট্যাটাস | বিজ্ঞপ্তি | ডেটা | উভয়ই |
|---|---|---|---|
| ফোরগ্রাউন্ড | onMessage |
onMessage |
onMessage |
| ব্যাকগ্রাউন্ড (সার্ভিস ওয়ার্কার) | onBackgroundMessage (বিজ্ঞপ্তি অটোমেটিক দেখানো হয়) |
onBackgroundMessage |
onBackgroundMessage (ডিসপ্লে বিজ্ঞপ্তি অটোমেটিক দেখানো হয়েছে) |
JavaScript দ্রুত শুরু নমুনা মেসেজ পাওয়ার জন্য প্রয়োজনীয় সব কোড ডেমোনস্ট্রেট করে।
আপনার ওয়েব অ্যাপ ফোরগ্রাউন্ডে থাকলে মেসেজ ম্যানেজ করা
onMessage ইভেন্ট পেতে, আপনার অ্যাপকে অবশ্যই firebase-messaging-sw.js-এ Firebase মেসেজিং সার্ভিস ওয়ার্কার সংজ্ঞায়িত করতে হবে।
বিকল্প হিসেবে, আপনি getToken(): Promise<string>-এর মাধ্যমে SDK-তে আগে থেকে থাকা কোনও সার্ভিস ওয়ার্কার প্রদান করতে পারেন।
Web
import { initializeApp } from "firebase/app"; import { getMessaging } from "firebase/messaging/sw"; // Initialize the Firebase app in the service worker by passing in // your app's Firebase config object. // https://firebase.google.com/docs/web/setup#config-object const firebaseApp = initializeApp({ apiKey: 'api-key', authDomain: 'project-id.firebaseapp.com', databaseURL: 'https://project-id.firebaseio.com', projectId: 'project-id', storageBucket: 'project-id.appspot.com', messagingSenderId: 'sender-id', appId: 'app-id', measurementId: 'G-measurement-id', }); // Retrieve an instance of Firebase Messaging so that it can handle background // messages. const messaging = getMessaging(firebaseApp);
Web
// Give the service worker access to Firebase Messaging. // Note that you can only use Firebase Messaging here. Other Firebase libraries // are not available in the service worker. // Replace 13.0.0 with latest version of the Firebase JS SDK. importScripts('https://www.gstatic.com/firebasejs/13.0.0/firebase-app-compat.js'); importScripts('https://www.gstatic.com/firebasejs/13.0.0/firebase-messaging-compat.js'); // Initialize the Firebase app in the service worker by passing in // your app's Firebase config object. // https://firebase.google.com/docs/web/setup#config-object firebase.initializeApp({ apiKey: 'api-key', authDomain: 'project-id.firebaseapp.com', databaseURL: 'https://project-id.firebaseio.com', projectId: 'project-id', storageBucket: 'project-id.appspot.com', messagingSenderId: 'sender-id', appId: 'app-id', measurementId: 'G-measurement-id', }); // Retrieve an instance of Firebase Messaging so that it can handle background // messages. const messaging = firebase.messaging();
আপনার অ্যাপ যখন ফোরগ্রাউন্ডে থাকে (ব্যবহারকারী আপনার ওয়েব পেজ দেখছেন), তখন আপনি সরাসরি পেজে ডেটা ও বিজ্ঞপ্তি পে-লোড পেতে পারেন।
Web
// Handle incoming messages. Called when: // - a message is received while the app has focus // - the user clicks on an app notification created by a service worker // `messaging.onBackgroundMessage` handler. import { getMessaging, onMessage } from "firebase/messaging"; const messaging = getMessaging(); onMessage(messaging, (payload) => { console.log('Message received. ', payload); // ... });
Web
// Handle incoming messages. Called when: // - a message is received while the app has focus // - the user clicks on an app notification created by a service worker // `messaging.onBackgroundMessage` handler. messaging.onMessage((payload) => { console.log('Message received. ', payload); // ... });
আপনার ওয়েব অ্যাপ ব্যাকগ্রাউন্ডে থাকলে মেসেজ ম্যানেজ করা
অ্যাপ ব্যাকগ্রাউন্ডে থাকাকালীন পাওয়া সব মেসেজ ব্রাউজারে একটি বিজ্ঞপ্তি দেখানোর ট্রিগার করে। আপনি এই বিজ্ঞপ্তির জন্য বিকল্প নির্দিষ্ট করতে পারেন, যেমন শিরোনাম বা ক্লিক অ্যাকশন, আপনার অ্যাপ সার্ভার থেকে পাঠানো অনুরোধে, অথবা ক্লায়েন্টে সার্ভিস ওয়ার্কার লজিক ব্যবহার করে।
অনুরোধ পাঠানোর সময় বিজ্ঞপ্তির বিকল্প সেট করুন
অ্যাপ সার্ভার থেকে পাঠানো বিজ্ঞপ্তি মেসেজের জন্য, FCM
JavaScript API,
fcm_options.link
কী সাপোর্ট করে। সাধারণত এটি আপনার ওয়েব অ্যাপের কোনও পৃষ্ঠায় সেট করা থাকে:
https://fcm.googleapis.com/v1/projects/<YOUR-PROJECT-ID>/messages:send
Content-Type: application/json
Authorization: bearer <YOUR-ACCESS-TOKEN>
{
"message": {
,
"notification": {
"title": "Background Message Title",
"body": "Background message body"
},
"webpush": {
"fcm_options": {
"link": "https://dummypage.com"
}
}
}
}
লিঙ্কের ভ্যালু যদি এমন কোনও পৃষ্ঠাকে পয়েন্ট করে যা আগে থেকেই ব্রাউজার ট্যাবে খোলা আছে, বিজ্ঞপ্তিতে ক্লিক করলে সেই ট্যাবটি ফোরগ্রাউন্ডে চলে আসে। পৃষ্ঠাটি আগে থেকে খোলা না থাকলে, বিজ্ঞপ্তিতে ক্লিক করলে নতুন ট্যাবে পৃষ্ঠাটি খুলে যায়।
ডেটা মেসেজে fcm_options.link কাজ করে না বলে, আপনাকে
সব ডেটা মেসেজে বিজ্ঞপ্তি পেলোড যোগ করার জন্য সাজেস্ট করা হয়। বিকল্প হিসেবে, আপনি সার্ভিস ওয়ার্কার ব্যবহার করে
বিজ্ঞপ্তি ম্যানেজ করতে পারবেন।
বিজ্ঞপ্তি ও ডেটা মেসেজের মধ্যে পার্থক্যের ব্যাখ্যা পেতে, মেসেজের ধরন দেখুন।
সার্ভিস ওয়ার্কারে বিজ্ঞপ্তির বিকল্প সেট করুন
ডেটা মেসেজের জন্য, আপনি পরিষেবা কর্মীতে বিজ্ঞপ্তি বিকল্প সেট করতে পারেন। প্রথমে, সার্ভিস ওয়ার্কারে আপনার অ্যাপ ইনিশিয়ালাইজ করুন:
Web
import { initializeApp } from "firebase/app"; import { getMessaging } from "firebase/messaging/sw"; // Initialize the Firebase app in the service worker by passing in // your app's Firebase config object. // https://firebase.google.com/docs/web/setup#config-object const firebaseApp = initializeApp({ apiKey: 'api-key', authDomain: 'project-id.firebaseapp.com', databaseURL: 'https://project-id.firebaseio.com', projectId: 'project-id', storageBucket: 'project-id.appspot.com', messagingSenderId: 'sender-id', appId: 'app-id', measurementId: 'G-measurement-id', }); // Retrieve an instance of Firebase Messaging so that it can handle background // messages. const messaging = getMessaging(firebaseApp);
Web
// Give the service worker access to Firebase Messaging. // Note that you can only use Firebase Messaging here. Other Firebase libraries // are not available in the service worker. // Replace 13.0.0 with latest version of the Firebase JS SDK. importScripts('https://www.gstatic.com/firebasejs/13.0.0/firebase-app-compat.js'); importScripts('https://www.gstatic.com/firebasejs/13.0.0/firebase-messaging-compat.js'); // Initialize the Firebase app in the service worker by passing in // your app's Firebase config object. // https://firebase.google.com/docs/web/setup#config-object firebase.initializeApp({ apiKey: 'api-key', authDomain: 'project-id.firebaseapp.com', databaseURL: 'https://project-id.firebaseio.com', projectId: 'project-id', storageBucket: 'project-id.appspot.com', messagingSenderId: 'sender-id', appId: 'app-id', measurementId: 'G-measurement-id', }); // Retrieve an instance of Firebase Messaging so that it can handle background // messages. const messaging = firebase.messaging();
বিকল্প সেট করতে, firebase-messaging-sw.js-এ
onBackgroundMessage নম্বরে কল করুন।
এই উদাহরণে, আমরা শীর্ষক, বডি ও আইকন ফিল্ড সহ একটি বিজ্ঞপ্তি তৈরি করি।
Web
import { getMessaging } from "firebase/messaging/sw"; import { onBackgroundMessage } from "firebase/messaging/sw"; const messaging = getMessaging(); onBackgroundMessage(messaging, (payload) => { console.log('[firebase-messaging-sw.js] Received background message ', payload); // Customize notification here const notificationTitle = 'Background Message Title'; const notificationOptions = { body: 'Background Message body.', icon: '/firebase-logo.png' }; self.registration.showNotification(notificationTitle, notificationOptions); });
Web
messaging.onBackgroundMessage((payload) => { console.log( '[firebase-messaging-sw.js] Received background message ', payload ); // Customize notification here const notificationTitle = 'Background Message Title'; const notificationOptions = { body: 'Background Message body.', icon: '/firebase-logo.png' }; self.registration.showNotification(notificationTitle, notificationOptions); });
বিজ্ঞপ্তির জন্য পেশাদার পদ্ধতি
যেসব ডেভেলপার FCM for Web-এর মাধ্যমে বিজ্ঞপ্তি পাঠান, তাদের জন্য সবচেয়ে গুরুত্বপূর্ণ বিষয় হল নির্ভুলতা ও প্রাসঙ্গিকতা। আপনার বিজ্ঞপ্তিগুলি নির্ভুল এবং প্রাসঙ্গিক রাখার জন্য এখানে কিছু নির্দিষ্ট সাজেশন দেওয়া হল:
- অর্থপূর্ণ ছবি পাঠাতে আইকন ফিল্ড ব্যবহার করুন। অনেক ব্যবহারের ক্ষেত্রে, এটি কোম্পানি বা অ্যাপের লোগো হওয়া উচিত যা আপনার ব্যবহারকারীরা অবিলম্বে চিনতে পারবেন। অথবা, কোনও চ্যাট অ্যাপ্লিকেশনের ক্ষেত্রে, এটি মেসেজ পাঠানো ব্যবহারকারীর প্রোফাইল ছবি হতে পারে।
- মেসেজের সঠিক প্রকৃতি প্রকাশ করতে শিরোনাম ফিল্ড ব্যবহার করুন। যেমন, "জিমি উত্তর দিয়েছেন" মেসেজটি "নতুন মেসেজ" মেসেজের চেয়ে আরও সুনির্দিষ্ট তথ্য প্রদান করে। আপনার কোম্পানি বা অ্যাপের নামের জন্য এই মূল্যবান জায়গা ব্যবহার করবেন না — সেই উদ্দেশ্যে আইকন ব্যবহার করুন।
- আপনার ওয়েবসাইটের নাম বা ডোমেন দেখানোর জন্য বিজ্ঞপ্তির শীর্ষক বা বডি ব্যবহার করবেন না; বিজ্ঞপ্তিতে আগে থেকেই আপনার ডোমেনের নাম থাকে।
fcm_options.linkযোগ করুন, সাধারণত ব্যবহারকারীকে আপনার ওয়েব অ্যাপে ফিরিয়ে নিয়ে যেতে এবং ব্রাউজারে সেটিকে ফোকাসে আনতে। খুব কম ক্ষেত্রে, যেখানে আপনি যে তথ্য জানাতে চান তা বিজ্ঞপ্তিতেই ফিট হয়ে যায়, সেখানে লিঙ্কের প্রয়োজন নাও হতে পারে।