| প্ল্যাটফর্ম বেছে নিন: | iOS+ Android ওয়েব Flutter Unity C++ |
পৃষ্ঠাটি ফোরগ্রাউন্ডে (ফোকাস করা আছে) আছে নাকি ব্যাকগ্রাউন্ডে, অন্য ট্যাবের
আড়ালে লুকানো আছে নাকি সম্পূর্ণ বন্ধ করা আছে, তার উপর নির্ভর করে মেসেজের আচরণ আলাদা আলাদা হয়। সব ক্ষেত্রেই, পৃষ্ঠাটিকে
onMessage
কলব্যাক হ্যান্ডেল করতে হবে, তবে ব্যাকগ্রাউন্ডে চলার ক্ষেত্রে আপনাকে
onBackgroundMessage
হ্যান্ডেল করতে বা ডিসপ্লে বিজ্ঞপ্তি কনফিগার করতে হতে পারে যাতে ব্যবহারকারী আপনার
ওয়েব অ্যাপকে ফোরগ্রাউন্ডে আনতে পারেন।
| অ্যাপের স্ট্যাটাস | বিজ্ঞপ্তি | ডেটা | উভয়ই |
|---|---|---|---|
| ফোরগ্রাউন্ড | onMessage |
onMessage |
onMessage |
| ব্যাকগ্রাউন্ড (সার্ভিস ওয়ার্কার) | onBackgroundMessage (বিজ্ঞপ্তি অটোমেটিক দেখানো হয়) |
onBackgroundMessage |
onBackgroundMessage (বিজ্ঞপ্তি অটোমেটিক দেখানো হয়) |
JavaScript দ্রুত শুরু নমুনা মেসেজ পাওয়ার জন্য প্রয়োজনীয় সব কোড ডেমোনস্ট্রেট করে।
আপনার ওয়েব অ্যাপ ফোরগ্রাউন্ডে থাকলে মেসেজ ম্যানেজ করা
onMessage ইভেন্ট পাওয়ার জন্য, আপনার অ্যাপকে অবশ্যই firebase-messaging-sw.js-এ Firebase মেসেজিং সার্ভিস ওয়ার্কার
সংজ্ঞায়িত করতে হবে।
বিকল্প হিসেবে, আপনি SDK-কে আগে থেকেই থাকা কোনও পরিষেবা কর্মী দিতে পারেন, এটি করতে
getToken(): Promise<string> ব্যবহার করুন।
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 10.13.2 with latest version of the Firebase JS SDK. importScripts('https://www.gstatic.com/firebasejs/10.13.2/firebase-app-compat.js'); importScripts('https://www.gstatic.com/firebasejs/10.13.2/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 10.13.2 with latest version of the Firebase JS SDK. importScripts('https://www.gstatic.com/firebasejs/10.13.2/firebase-app-compat.js'); importScripts('https://www.gstatic.com/firebasejs/10.13.2/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-এর মাধ্যমে বিজ্ঞপ্তি পাঠানো ডেভেলপারদের জন্য, সবচেয়ে গুরুত্বপূর্ণ বিষয় হল নির্ভুলতা ও প্রাসঙ্গিকতা। আপনার বিজ্ঞপ্তি যাতে সঠিক ও প্রাসঙ্গিক হয়, সেই জন্য এখানে কিছু নির্দিষ্ট সাজেশন দেওয়া হল:
- অর্থপূর্ণ ছবি পাঠানোর জন্য আইকন ফিল্ড ব্যবহার করুন। অনেক ব্যবহারের ক্ষেত্রে, এটি এমন কোনও কোম্পানি বা অ্যাপ লোগো হওয়া উচিত যা আপনার ব্যবহারকারীরা অবিলম্বে চিনতে পারবেন। অথবা, কোনও চ্যাট অ্যাপ্লিকেশনের ক্ষেত্রে, এটি হতে পারে প্রেরক ব্যবহারকারীর প্রোফাইল ছবি।
- মেসেজের সঠিক প্রকৃতি বোঝাতে, শীর্ষক ফিল্ড ব্যবহার করুন। যেমন, "জিমি উত্তর দিয়েছে" বাক্যটি "নতুন মেসেজ" বাক্যটির চেয়ে বেশি সুনির্দিষ্ট তথ্য প্রদান করে। আপনার কোম্পানি বা অ্যাপের নাম এই গুরুত্বপূর্ণ জায়গায় লিখবেন না — সেই উদ্দেশ্যে আইকন ব্যবহার করুন।
- বিজ্ঞপ্তির শিরোনাম বা বডিতে আপনার ওয়েবসাইটের নাম বা ডোমেন দেখাবেন না; বিজ্ঞপ্তিতে আগেই আপনার ডোমেনের নাম থাকে।
fcm_options.linkযোগ করুন, সাধারণত ব্যবহারকারীকে আপনার ওয়েব অ্যাপের সাথে লিঙ্ক করতে এবং ব্রাউজারে সেটি ফোকাসে আনতে এটি করা হয়। খুবই কম ক্ষেত্রে, বিজ্ঞপ্তি মারফত জানানোর জন্য প্রয়োজনীয় সব তথ্য যদি বিজ্ঞপ্তিতেই দেওয়া যায়, তাহলে লিঙ্কের প্রয়োজন নাও হতে পারে।