एंड्रॉइड में फायरबेस क्लाउड मैसेजिंग (FCM) सेटअप
यह गाइड एक आधुनिक एंड्रॉइड ऐप (Kotlin/Java) में FCM को एंड-टू-एंड कवर करता है: निर्भरताएं, मैनिफेस्ट, टोकन हैंडलिंग, फोरग्राउंड/बैकग्राउंड संदेश, और नोटिफिकेशन चैनल (API 26+)
1. निर्भरताएं (build.gradle.kts / .gradle)
dependencies {
// Firebase BOM संस्करण प्रबंधन के लिए
implementation platform("com.google.firebase:firebase-bom:32.8.0")
implementation "com.google.firebase:firebase-messaging-ktx"
implementation "com.google.firebase:firebase-analytics-ktx"
}
2. google-services.json
फायरबेस कंसोल → प्रोजेक्ट सेटिंग्स → अपना ऐप चुनें → google-services.json डाउनलोड करें → app/ में रखें।
3. मैनिफेस्ट (AndroidManifest.xml)
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.fcmdemo">
<!-- नोटिफिकेशन पोस्ट करने के लिए आवश्यक (API 33+) -->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.FcmDemo">
<!-- डिफ़ॉल्ट नोटिफिकेशन चैनल (फोरग्राउंड संदेशों के लिए) -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="default_channel" />
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
4. एप्लिकेशन क्लास - नोटिफिकेशन चैनल बनाएं
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
createNotificationChannels();
}
private void createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel defaultChannel = new NotificationChannel(
"default_channel", "सामान्य नोटिफिकेशन", NotificationManager.IMPORTANCE_DEFAULT);
defaultChannel.setDescription("ऐप नोटिफिकेशन के लिए डिफ़ॉल्ट चैनल");
NotificationChannel highChannel = new NotificationChannel(
"high_priority", "उच्च प्राथमिकता", NotificationManager.IMPORTANCE_HIGH);
highChannel.setDescription("महत्वपूर्ण अलर्ट");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannels(Arrays.asList(defaultChannel, highChannel));
}
}
}
5. FirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCMService";
@Override
public void onNewToken(@NonNull String token) {
super.onNewToken(token);
Log.d(TAG, "नया FCM टोकन: " + token);
// अपने ऐप सर्वर पर टोकन भेजें यदि आवश्यक हो
// sendTokenToServer(token);
}
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "संदेश प्राप्त: " + remoteMessage.getFrom());
// डेटा पेलोड (हमेशा onMessageReceived में वितरित)
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "डेटा पेलोड: " + remoteMessage.getData());
handleDataPayload(remoteMessage.getData());
}
// नोटिफिकेशन पेलोड (केवल फोरग्राउंड में यहां वितरित; बैकग्राउंड → सिस्टम ट्रे)
if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
Log.d(TAG, "नोटिफिकेशन: " + title + " / " + body);
if (isAppInForeground()) {
showLocalNotification(title, body, "default_channel");
}
}
}
private boolean isAppInForeground() {
// सरल जांच; उत्पादन के लिए ProcessLifecycleOwner का उपयोग करें
return true; // प्लेसहोल्डर
}
private void handleDataPayload(Map data) {
// कस्टम डेटा संभालें (नेविगेशन, सिंक, आदि)
}
private void showLocalNotification(String title, String body, String channelId) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(body)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
NotificationManagerCompat.from(this).notify((int) System.currentTimeMillis(), builder.build());
}
}
6. मुख्य गतिविधि - रनटाइम परमिशन (API 33+)
public class MainActivity extends AppCompatActivity {
private static final int PERM_NOTIFICATION = 1001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.POST_NOTIFICATIONS}, PERM_NOTIFICATION);
}
}
// टोकन लॉग करें (डीबग)
FirebaseMessaging.getInstance().getToken()
.addOnSuccessListener(token -> Log.d("FCM", "वर्तमान टोकन: " + token));
}
}
7. कंसोल से टेस्ट भेजें
- फायरबेस कंसोल → **Messaging** → **New notification**।
- शीर्षक/बॉडी दर्ज करें → **Next**।
- अपना ऐप चुनें → **Next** → **Publish**।
- डिवाइस पर नोटिफिकेशन दिखाई देता है (फोरग्राउंड: कस्टम; बैकग्राउंड: सिस्टम)।
उन्नत विषय
- टॉपिक मैसेजिंग:
FirebaseMessaging.getInstance().subscribeToTopic("news")। - डेटा-ओनली संदेश: कंसोल में "Additional options" → कस्टम डेटा जोड़ें;
onMessageReceivedमें संभालें। - इमेज नोटिफिकेशन: पेलोड में
imageURL शामिल करें; FCM SDK स्वचालित रूप से डाउनलोड/प्रदर्शित करता है। - टोकन मॉनिटरिंग:
onNewTokenमें, अपने बैकएंड पर POST करें उपयोगकर्ता-डिवाइस मैपिंग के लिए।
सामान्य समस्याएं
| समस्या | समाधान |
|---|---|
| कोई टोकन जनरेट नहीं हुआ | google-services.json सही मॉड्यूल में है, इंटरनेट अनुमति, Google Play सेवाएं अपडेटेड। |
| फोरग्राउंड में नोटिफिकेशन नहीं | onMessageReceived में लोकल नोटिफिकेशन पोस्ट करें (ऊपर देखें)। |
| API 33+ पर क्रैश | POST_NOTIFICATIONS रनटाइम परमिशन का अनुरोध करें। |
| बैकग्राउंड में डेटा संदेश प्राप्त नहीं हुआ | डेटा-ओनली संदेश बैकग्राउंड में onMessageReceived में वितरित किए जाते हैं (कुछ OEMs प्रतिबंधित करते हैं)। |
मूल उदाहरण की समीक्षा
मूल कोड एक नंगे हड्डियों वाली सेवा दिखाता था जिसमें हार्डकोडेड चैनल ID, कोई परमिशन हैंडलिंग नहीं, कोई एप्लिकेशन-क्लास चैनल निर्माण नहीं, और लिगेसी सपोर्ट लाइब्रेरी आयात थे। ऊपर दिया गया संस्करण उत्पादन-तैयार है।