FCM पुश नोटिफिकेशन पर एक्टिविटी लॉन्च करना
जब सर्वर से फायरबेस क्लाउड मैसेजिंग (FCM) पुश नोटिफिकेशन आता है, तो यूजर के टैप करने पर मनचाही एक्टिविटी खोलने के लिए FirebaseMessagingService में onMessageReceived() ओवरराइड करें।
चरण 1: नया प्रोजेक्ट + फायरबेस सेटअप
Android Studio में प्रोजेक्ट बनाएं, फिर Tools → Firebase → Cloud Messaging से फायरबेस जोड़ें। google-services.json ऐप मॉड्यूल में डालें।
चरण 2: MyFirebaseMessagingService बनाएं
src/MyFirebaseMessagingService.java — यह सर्विस पुश मैसेज प्राप्त करती है और लोकल नोटिफिकेशन बनाकर दिखाती है:
package app.tutorialspoint.com.notifyme;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String NOTIFICATION_CHANNEL_ID = "10001";
private final static String default_notification_channel_id = "default";
@Override
public void onNewToken(String s) {
super.onNewToken(s);
// नया FCM टोकन मिला — अपने सर्वर पर भेजें
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// जिस एक्टिविटी को खोलना है उसका इंटेंट
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.putExtra("NotificationMessage", "I am from Notification");
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
// नोटिफिकेशन बनाएं
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), default_notification_channel_id)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Test")
.setContentText("Hello! This is my first push notification")
.setContentIntent(resultIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
assert mNotificationManager != null;
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
}
}
चरण 3: मैनिफेस्ट में सर्विस रजिस्टर करें
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
मुख्य बिंदु
onMessageReceived() तभी कॉल होता है जब ऐप फोरग्राउंड में हो, या डेटा-ओनली मैसेज हो- नोटिफिकेशन मैसेज (notification payload) होने पर सिस्टम खुद नोटिफिकेशन दिखाता है —
onMessageReceived()नहीं कॉल होता - इसलिए सर्वर से डेटा पेलोड भेजें (notification ऑब्जेक्ट के बजाय data ऑब्जेक्ट)
PendingIntent.FLAG_IMMUTABLE(API 23+) याFLAG_MUTABLEजरूर सेट करें
सर्वर से JSON पेलोड उदाहरण
{
"to": "/topics/all",
"data": {
"title": "नया संदेश",
"body": "आपके पास नया मैसेज है",
"activity": "ChatActivity"
}
}
ऐप को फोरग्राउंड में रखकर टेस्ट करें, या डेटा-ओनली पुश भेजें।