Android पर Firebase क्लाउड से मैसेज क्लाइंट ऐप्लिकेशन सेट अप करना

FCM क्लाइंट के लिए ज़रूरी है कि Android 4.4 या उसके बाद के वर्शन पर चल रहे डिवाइस में Google Play Store ऐप्लिकेशन इंस्टॉल हो या ऐसा एम्युलेटर हो जिस पर Google API के साथ Android 4.4 वर्शन चल रहा हो. ध्यान दें कि सिर्फ़ Google Play Store से अपने Android ऐप्लिकेशन डिप्लॉय नहीं किए जा सकते.

SDK टूल सेट अप करना

अगर आपने अपने ऐप्लिकेशन के लिए Firebase की दूसरी सुविधाएं पहले ही चालू कर रखी हैं, तो इस सेक्शन में ऐसे टास्क शामिल होंगे जिन्हें शायद आपने पूरा कर लिया हो. अगर आपने Firebase की दूसरी सुविधाएं पहले से चालू नहीं की हैं, तो अपने Android प्रोजेक्ट में Firebase जोड़ें

अपने ऐप्लिकेशन मेनिफ़ेस्ट में बदलाव करना

अपने ऐप्लिकेशन के मेनिफ़ेस्ट में यह जानकारी जोड़ें:

  • ऐसी सेवा जो FirebaseMessagingService को बढ़ाती है. ऐसा तब ज़रूरी होता है, जब आपको बैकग्राउंड में ऐप्लिकेशन की सूचनाएं पाने के अलावा, मैसेज मैनेज करने का अन्य काम करना हो. फ़ोरग्राउंड में दिखने वाले ऐप्लिकेशन में सूचनाएं पाने, डेटा पेलोड पाने, अपस्ट्रीम मैसेज भेजने वगैरह के लिए, आपको इस सेवा को बढ़ाना होगा.
  • <service
        android:name=".java.MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
  • (ज़रूरी नहीं) ऐप्लिकेशन कॉम्पोनेंट में, डिफ़ॉल्ट सूचना आइकॉन और रंग सेट करने के लिए मेटाडेटा एलिमेंट. जब भी इनकमिंग मैसेज साफ़ तौर पर आइकॉन या रंग सेट नहीं करते, तब Android इन वैल्यू का इस्तेमाल करता है.
  • <!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
         See README(https://goo.gl/l4GJaQ) for more. -->
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_stat_ic_notification" />
    <!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
         notification message. See README(https://goo.gl/6BKBk7) for more. -->
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />
  • (ज़रूरी नहीं) Android 8.0 (एपीआई लेवल 26) और उसके बाद के वर्शन पर, सूचना देने वाले चैनल काम करते हैं और उनका सुझाव देते हैं. FCM, बुनियादी सेटिंग के साथ सूचना का डिफ़ॉल्ट चैनल देता है. अगर आपको अपना डिफ़ॉल्ट चैनल बनाना और उसका इस्तेमाल करना है, तो default_notification_channel_id को अपने सूचना चैनल ऑब्जेक्ट के आईडी पर दिखाए गए तरीके के हिसाब से सेट करें. जब भी इनकमिंग मैसेज सूचना चैनल को सेट नहीं करेंगे, तब FCM इस वैल्यू का इस्तेमाल करेगा. ज़्यादा जानने के लिए, सूचना चैनलों को मैनेज करना देखें.
  • <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="@string/default_notification_channel_id" />

Android 13 के बाद के वर्शन पर रनटाइम के लिए सूचना की अनुमति का अनुरोध करना

Android 13 में, सूचनाएं दिखाने के लिए रनटाइम की नई अनुमति लॉन्च की गई है. इसका असर Android 13 या उसके बाद के वर्शन पर चल रहे उन सभी ऐप्लिकेशन पर पड़ता है जो FCM सूचनाओं का इस्तेमाल करते हैं.

डिफ़ॉल्ट रूप से, FCM SDK टूल (23.0.6 या इसके बाद के वर्शन) में, मेनिफ़ेस्ट में बताई गई POST_NOTIFICATIONS अनुमति शामिल होती है. हालांकि, आपके ऐप्लिकेशन को कॉन्सटेंट, android.permission.POST_NOTIFICATIONS के ज़रिए, इस अनुमति के रनटाइम वर्शन का अनुरोध भी करना होगा. जब तक उपयोगकर्ता यह अनुमति नहीं देता, तब तक आपके ऐप्लिकेशन को सूचनाएं दिखाने की अनुमति नहीं दी जाएगी.

नई रनटाइम अनुमति का अनुरोध करने के लिए:

Kotlin+KTX

// Declare the launcher at the top of your Activity/Fragment:
private val requestPermissionLauncher = registerForActivityResult(
    ActivityResultContracts.RequestPermission(),
) { isGranted: Boolean ->
    if (isGranted) {
        // FCM SDK (and your app) can post notifications.
    } else {
        // TODO: Inform user that that your app will not show notifications.
    }
}

private fun askNotificationPermission() {
    // This is only necessary for API level >= 33 (TIRAMISU)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
            PackageManager.PERMISSION_GRANTED
        ) {
            // FCM SDK (and your app) can post notifications.
        } else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
            // TODO: display an educational UI explaining to the user the features that will be enabled
            //       by them granting the POST_NOTIFICATION permission. This UI should provide the user
            //       "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission.
            //       If the user selects "No thanks," allow the user to continue without notifications.
        } else {
            // Directly ask for the permission
            requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
        }
    }
}

Java

// Declare the launcher at the top of your Activity/Fragment:
private final ActivityResultLauncher<String> requestPermissionLauncher =
        registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
            if (isGranted) {
                // FCM SDK (and your app) can post notifications.
            } else {
                // TODO: Inform user that that your app will not show notifications.
            }
        });

private void askNotificationPermission() {
    // This is only necessary for API level >= 33 (TIRAMISU)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
                PackageManager.PERMISSION_GRANTED) {
            // FCM SDK (and your app) can post notifications.
        } else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
            // TODO: display an educational UI explaining to the user the features that will be enabled
            //       by them granting the POST_NOTIFICATION permission. This UI should provide the user
            //       "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission.
            //       If the user selects "No thanks," allow the user to continue without notifications.
        } else {
            // Directly ask for the permission
            requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
        }
    }
}

आम तौर पर, आपको एक यूज़र इंटरफ़ेस (यूआई) दिखाना चाहिए, जिसमें उन सुविधाओं के बारे में जानकारी दी गई हो जो ऐप्लिकेशन को सूचनाएं पोस्ट करने की अनुमति देने पर चालू हो जाएंगी. इस यूज़र इंटरफ़ेस (यूआई) में, सहमति देने या न देने के विकल्प दिए जाने चाहिए. जैसे, OK और रहने दें बटन. अगर उपयोगकर्ता ठीक है का विकल्प चुनता है, तो सीधे अनुमति के लिए अनुरोध करें. अगर उपयोगकर्ता रहने दें का विकल्प चुनता है, तो उपयोगकर्ता को बिना किसी सूचना के जारी रखने की अनुमति दें.

आपके ऐप्लिकेशन को उपयोगकर्ता से POST_NOTIFICATIONS की अनुमति का अनुरोध कब करना चाहिए, इस बारे में सबसे सही तरीकों के बारे में जानने के लिए, सूचना रनटाइम की अनुमति देखें.

Android 12L (एपीआई लेवल 32) या इससे पहले के वर्शन को टारगेट करने वाले ऐप्लिकेशन के लिए, सूचना की अनुमतियां

जब आपका ऐप्लिकेशन पहली बार कोई सूचना चैनल बनाता है, तो Android अपने-आप उपयोगकर्ता से अनुमति मांगता है. ऐसा तब होता है, जब ऐप्लिकेशन फ़ोरग्राउंड में होता है. हालांकि, चैनल बनाने के समय और अनुमति के अनुरोधों को लेकर कुछ अहम चेतावनियां हैं:

  • अगर आपका ऐप्लिकेशन, बैकग्राउंड में चलने पर सूचना देने वाला पहला चैनल बनाता है (जो FCM सूचना पाने पर FCM SDK भेजता है), तो Android सूचना दिखाने की अनुमति नहीं देगा. साथ ही, अगली बार आपका ऐप्लिकेशन खोले जाने तक उपयोगकर्ता को सूचना की अनुमति का अनुरोध नहीं करेगा. इसका मतलब है कि आपका ऐप्लिकेशन खुलने और उपयोगकर्ता की अनुमति स्वीकार करने से पहले मिली सभी सूचनाएं हट जाएंगी.
  • हमारा सुझाव है कि आप Android 13 और इसके बाद के वर्शन को टारगेट करने के लिए, अपने ऐप्लिकेशन को अपडेट करें. इससे, प्लैटफ़ॉर्म के एपीआई का फ़ायदा लिया जा सकेगा और अनुमति का अनुरोध किया जा सकेगा. अगर ऐसा करना मुमकिन नहीं है, तो आपके ऐप्लिकेशन को सूचना भेजने की अनुमति वाला डायलॉग बॉक्स ट्रिगर करने के लिए, ऐप्लिकेशन में कोई भी सूचना भेजने से पहले सूचना चैनल बनाना चाहिए और यह पक्का करना चाहिए कि कोई भी सूचना न भेजी जाए. ज़्यादा जानकारी के लिए, सूचना पाने की अनुमति से जुड़े सबसे सही तरीके देखें.

ज़रूरी नहीं: POST_NOTIFICATIONS की अनुमति हटाएं

डिफ़ॉल्ट रूप से, FCM SDK टूल में POST_NOTIFICATIONS की अनुमति शामिल होती है. अगर आपका ऐप्लिकेशन सूचना वाले मैसेज (चाहे FCM की सूचनाओं के ज़रिए, किसी दूसरे SDK टूल के ज़रिए या सीधे आपके ऐप्लिकेशन से पोस्ट किया गया हो) का इस्तेमाल नहीं करता है और आपको अपने ऐप्लिकेशन में अनुमति शामिल नहीं करनी है, तो मेनिफ़ेस्ट मर्जर remove मार्कर का इस्तेमाल करके उसे हटाया जा सकता है. ध्यान रखें कि इस अनुमति को हटाने से, सिर्फ़ FCM सूचनाएं ही नहीं, बल्कि सभी सूचनाएं भी नहीं दिखेंगी. अपने ऐप्लिकेशन की मेनिफ़ेस्ट फ़ाइल में इन्हें जोड़ें:

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" tools:node="remove"/>

डिवाइस रजिस्ट्रेशन टोकन ऐक्सेस करना

आपके ऐप्लिकेशन के शुरुआती स्टार्टअप पर, FCM SDK टूल, क्लाइंट ऐप्लिकेशन इंस्टेंस के लिए रजिस्ट्रेशन टोकन जनरेट करता है. अगर आपको किसी एक डिवाइस को टारगेट करना है या डिवाइस ग्रुप बनाना है, तो आपको FirebaseMessagingService को बढ़ाकर और onNewToken को बदलकर, इस टोकन को ऐक्सेस करना होगा.

इस सेक्शन में बताया गया है कि टोकन को कैसे वापस पाएं और उसमें हुए बदलावों को कैसे मॉनिटर करें. शुरुआती स्टार्टअप के बाद टोकन को रोटेट किया जा सकता है. इसलिए, हमारा सुझाव है कि आप सबसे नए अपडेट किए गए रजिस्ट्रेशन टोकन को फिर से वापस पाएं.

रजिस्ट्रेशन टोकन में बदलाव हो सकता है, जब:

  • ऐप्लिकेशन को नए डिवाइस पर वापस लाया गया है
  • उपयोगकर्ता ने ऐप्लिकेशन को अनइंस्टॉल किया या फिर से इंस्टॉल किया
  • उपयोगकर्ता, ऐप्लिकेशन का डेटा मिटाता है.

मौजूदा रजिस्ट्रेशन टोकन वापस पाएं

जब आपको मौजूदा टोकन को फिर से पाना हो, तो FirebaseMessaging.getInstance().getToken() को कॉल करें:

Kotlin+KTX

FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
    if (!task.isSuccessful) {
        Log.w(TAG, "Fetching FCM registration token failed", task.exception)
        return@OnCompleteListener
    }

    // Get new FCM registration token
    val token = task.result

    // Log and toast
    val msg = getString(R.string.msg_token_fmt, token)
    Log.d(TAG, msg)
    Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
})

Java

FirebaseMessaging.getInstance().getToken()
    .addOnCompleteListener(new OnCompleteListener<String>() {
        @Override
        public void onComplete(@NonNull Task<String> task) {
          if (!task.isSuccessful()) {
            Log.w(TAG, "Fetching FCM registration token failed", task.getException());
            return;
          }

          // Get new FCM registration token
          String token = task.getResult();

          // Log and toast
          String msg = getString(R.string.msg_token_fmt, token);
          Log.d(TAG, msg);
          Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    });

टोकन जनरेट करने पर नज़र रखें

नया टोकन जनरेट होने पर, onNewToken कॉलबैक फ़ायर हो जाता है.

Kotlin+KTX

/**
 * Called if the FCM registration token is updated. This may occur if the security of
 * the previous token had been compromised. Note that this is called when the
 * FCM registration token is initially generated so this is where you would retrieve the token.
 */
override fun onNewToken(token: String) {
    Log.d(TAG, "Refreshed token: $token")

    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // FCM registration token to your app server.
    sendRegistrationToServer(token)
}

Java

/**
 * There are two scenarios when onNewToken is called:
 * 1) When a new token is generated on initial app startup
 * 2) Whenever an existing token is changed
 * Under #2, there are three scenarios when the existing token is changed:
 * A) App is restored to a new device
 * B) User uninstalls/reinstalls the app
 * C) User clears app data
 */
@Override
public void onNewToken(@NonNull String token) {
    Log.d(TAG, "Refreshed token: " + token);

    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // FCM registration token to your app server.
    sendRegistrationToServer(token);
}

टोकन मिलने के बाद, उसे अपने ऐप्लिकेशन सर्वर पर भेजा जा सकता है और अपने पसंदीदा तरीके से इसे सेव किया जा सकता है.

Google Play services की जानकारी देखना

Play सेवाएं SDK टूल पर निर्भर ऐप्लिकेशन को Google Play सेवाओं की सुविधाएं ऐक्सेस करने से पहले हमेशा इस बात की जांच करनी चाहिए कि डिवाइस Google Play services के साथ काम करता है या नहीं. हमारा सुझाव है कि आप दो जगहों पर ऐसा करें: मुख्य गतिविधि के onCreate() तरीके से और इसके onResume() वाले तरीके में. onCreate() की जांच से यह पक्का होता है कि जांच पूरी हुए बिना ऐप्लिकेशन का इस्तेमाल नहीं किया जा सकता. जांच onResume() से यह पक्का होता है कि अगर उपयोगकर्ता, 'वापस जाएं' बटन जैसे किसी दूसरे तरीके से, ऐप्लिकेशन पर वापस आता है, तब भी जांच होती है.

अगर डिवाइस में Google Play services के साथ काम करने वाला वर्शन नहीं है, तो आपका ऐप्लिकेशन GoogleApiAvailability.makeGooglePlayServicesAvailable() पर कॉल कर सकता है. इससे लोगों को Play Store से Google Play services डाउनलोड करने की अनुमति मिलेगी.

अपने-आप शुरू होने से रोकें

FCM रजिस्ट्रेशन टोकन जनरेट होने पर लाइब्रेरी, आइडेंटिफ़ायर और कॉन्फ़िगरेशन डेटा को Firebase पर अपलोड करती है. अगर आपको टोकन अपने-आप जनरेट होने से रोकना है, तो अपने AndroidManifest.xml में ये मेटाडेटा वैल्यू जोड़कर, Analytics कलेक्शन और FCM अपने-आप शुरू होने की सुविधा (आपको दोनों को बंद करना होगा) बंद करें:

<meta-data
    android:name="firebase_messaging_auto_init_enabled"
    android:value="false" />
<meta-data
    android:name="firebase_analytics_collection_enabled"
    android:value="false" />

FCM ऑटो-इन सुविधा को फिर से चालू करने के लिए, रनटाइम कॉल करें:

Kotlin+KTX

Firebase.messaging.isAutoInitEnabled = true

Java

FirebaseMessaging.getInstance().setAutoInitEnabled(true);

Analytics कलेक्शन को फिर से चालू करने के लिए, FirebaseAnalytics क्लास के setAnalyticsCollectionEnabled() तरीके को कॉल करें. उदाहरण के लिए:

setAnalyticsCollectionEnabled(true);

ये वैल्यू, एक बार सेट होने के बाद ऐप्लिकेशन के रीस्टार्ट होने पर भी लागू रहती हैं.

अगले चरण

क्लाइंट ऐप्लिकेशन सेट अप हो जाने के बाद, सूचना बनाने वाले टूल की मदद से, डाउनस्ट्रीम मैसेज भेजे जा सकते हैं. यह सुविधा, क्विकस्टार्ट सैंपल में दी गई है. इसे डाउनलोड करके चलाया जा सकता है और इसकी समीक्षा की जा सकती है.

अपने ऐप्लिकेशन में अन्य और बेहतर सेटिंग जोड़ने के लिए, आपके पास इंटेंट फ़िल्टर का एलान करने और आने वाले मैसेज का जवाब देने के लिए कोई गतिविधि लागू करने का विकल्प होता है. ज़्यादा जानकारी के लिए, ऐप्लिकेशन सर्वर से मैसेज भेजने की गाइड देखें:

ध्यान रखें कि इन सुविधाओं का फ़ायदा पाने के लिए, आपको एक सर्वर लागू करना और सर्वर प्रोटोकॉल (एचटीटीपी या XMPP) या एडमिन SDK को लागू करना होगा.