वेब ऐप्लिकेशन में मैसेज पाना

प्लैटफ़ॉर्म चुनें: iOS+ Android वेब Flutter Unity C++


मैसेज का व्यवहार इस बात पर निर्भर करता है कि पेज फ़ोरग्राउंड (फ़ोकस में है) में है या बैकग्राउंड में, अन्य टैब के पीछे छिपा हुआ है या पूरी तरह से बंद है. सभी मामलों में, पेज को onMessage कॉलबैक को हैंडल करना होगा. हालांकि, बैकग्राउंड के मामलों में आपको onBackgroundMessage को भी हैंडल करना पड़ सकता है या डिसप्ले नोटिफ़िकेशन को कॉन्फ़िगर करना पड़ सकता है, ताकि उपयोगकर्ता आपके वेब ऐप्लिकेशन को फ़ोरग्राउंड में ला सके.

ऐप्लिकेशन की स्थिति सूचना डेटा दोनों
फ़ोरग्राउंड onMessage onMessage onMessage
बैकग्राउंड (सर्विस वर्कर) onBackgroundMessage (डिसप्ले सूचना अपने-आप दिखती है) onBackgroundMessage onBackgroundMessage (डिसप्ले सूचना अपने-आप दिखती है)

JavaScript क्विकस्टार्ट सैंपल में, मैसेज पाने के लिए ज़रूरी सभी कोड दिखाए गए हैं.

जब आपका वेब ऐप्लिकेशन फ़ोरग्राउंड में हो, तब मैसेज मैनेज करना

onMessage इवेंट पाने के लिए, आपके ऐप्लिकेशन को firebase-messaging-sw.js में Firebase मैसेजिंग सर्विस वर्कर को तय करना होगा. इसके अलावा, 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 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 के ज़रिए सूचनाएं भेजने वाले डेवलपर के लिए, सबसे ज़रूरी बातें हैं: सटीक और काम की जानकारी देना. सूचनाओं को सटीक और काम का बनाए रखने के लिए, यहां कुछ सुझाव दिए गए हैं:

  • कोई काम की इमेज भेजने के लिए, आइकॉन फ़ील्ड का इस्तेमाल करें. कई मामलों में, यह आपकी कंपनी या ऐप्लिकेशन का लोगो होना चाहिए, ताकि आपके उपयोगकर्ता इसे तुरंत पहचान सकें. इसके अलावा, चैट ऐप्लिकेशन के लिए, यह मैसेज भेजने वाले उपयोगकर्ता की प्रोफ़ाइल इमेज हो सकती है.
  • मैसेज के बारे में सटीक जानकारी देने के लिए, टाइटल फ़ील्ड का इस्तेमाल करें. उदाहरण के लिए, "जिमी ने जवाब दिया" से "नया मैसेज" के मुकाबले ज़्यादा सटीक जानकारी मिलती है. इस अहम जगह का इस्तेमाल, अपनी कंपनी या ऐप्लिकेशन का नाम दिखाने के लिए न करें. इसके लिए, आइकॉन का इस्तेमाल करें.
  • अपनी वेबसाइट का नाम या डोमेन दिखाने के लिए, सूचना के टाइटल या मुख्य हिस्से का इस्तेमाल न करें. सूचनाओं में पहले से ही आपका डोमेन नेम शामिल होता है.
  • fcm_options.link जोड़ें. आम तौर पर, इसका इस्तेमाल उपयोगकर्ता को आपके वेब ऐप्लिकेशन पर वापस ले जाने और उसे ब्राउज़र में फ़ोकस में लाने के लिए किया जाता है. कुछ मामलों में, आपको जो जानकारी देनी है वह सूचना में ही शामिल हो सकती है. ऐसे में, आपको लिंक की ज़रूरत नहीं पड़ सकती.