पुश प्राप्त होने पर लोकल नोटिफिकेशन बनाना
जब FCM पुश नोटिफिकेशन आता है, तो FirebaseMessagingService.onMessageReceived() में आप अतिरिक्त लोकल नोटिफिकेशन बना सकते हैं (उदाहरण: बैज अपडेट, अलग चैनल में लॉग, साइलेंट नोटिफिकेशन)।
चरण 1: प्रोजेक्ट + फायरबेस सेटअप
फायरबेस कंसोल में प्रोजेक्ट बनाएं, google-services.json ऐड करें, निर्भरताएं जोड़ें:
implementation 'com.google.firebase:firebase-messaging:23.0.0'
चरण 2: लेआउट
सामान्य बटन वाला लेआउट (लोकल टेस्ट के लिए)।
चरण 3: MainActivity — लोकल नोटिफिकेशन बनाने का मेथड
package app.tutorialspoint.com.notifyme;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public static final String NOTIFICATION_CHANNEL_ID = "10001";
private final static String default_notification_channel_id = "default";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onNewIntent(getIntent());
}
public void createNotification(View view) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this, default_notification_channel_id);
mBuilder.setContentTitle("Notify Me");
mBuilder.setContentText("Something important!");
mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
mBuilder.setAutoCancel(true);
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());
}
}
चरण 4: MyFirebaseMessagingService — पुश पर एक्स्ट्रा नोटिफिकेशन
package app.tutorialspoint.com.notifyme;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Notification;
import android.content.Context;
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";
private final static String EXTRA_CHANNEL_ID = "extra_channel";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// 1. मुख्य नोटिफिकेशन (पुश पेलोड से)
showMainNotification(remoteMessage);
// 2. एक्स्ट्रा नोटिफिकेशन (लोकल लॉजिक)
showExtraNotification(remoteMessage);
}
private void showMainNotification(RemoteMessage message) {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, default_notification_channel_id)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(message.getNotification() != null ? message.getNotification().getTitle() : "New Message")
.setContentText(message.getNotification() != null ? message.getNotification().getBody() : "Check the app")
.setAutoCancel(true);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
createChannelIfNeeded(manager, default_notification_channel_id, "Main", NotificationManager.IMPORTANCE_HIGH);
builder.setChannelId(default_notification_channel_id);
}
manager.notify((int) System.currentTimeMillis(), builder.build());
}
private void showExtraNotification(RemoteMessage message) {
// उदाहरण: साइलेंट लॉग नोटिफिकेशन, या बैज अपडेट, या अलग चैनल में
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, EXTRA_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Push Received")
.setContentText("Extra notification at " + System.currentTimeMillis())
.setPriority(NotificationCompat.PRIORITY_LOW)
.setAutoCancel(true);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
createChannelIfNeeded(manager, EXTRA_CHANNEL_ID, "Extra", NotificationManager.IMPORTANCE_LOW);
builder.setChannelId(EXTRA_CHANNEL_ID);
}
manager.notify((int) System.currentTimeMillis() + 1, builder.build());
}
private void createChannelIfNeeded(NotificationManager manager, String id, String name, int importance) {
if (manager.getNotificationChannel(id) == null) {
NotificationChannel channel = new NotificationChannel(id, name, importance);
manager.createNotificationChannel(channel);
}
}
}
चरण 5: मैनिफेस्ट में दोनों सर्विस रजिस्टर करें
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
कब उपयोगी है?
- पुश से अलग चैनल में "लॉग" नोटिफिकेशन
- बैज काउंट अपडेट करने के लिए साइलेंट नोटिफिकेशन
- पुश पेलोड के आधार पर कंडीशनल नोटिफिकेशन
- मीडिया प्लेबैक कंट्रोल नोटिफिकेशन अलग से दिखाना
⚠️ ध्यान दें
onMessageReceived()केवल तब कॉल होता है जब ऐप फोरग्राउंड में हो, या डेटा-ओनली पुश हो- नोटिफिकेशन पेलोड (notification ऑब्जेक्ट) वाले पुश पर सिस्टम खुद नोटिफिकेशन दिखाता है —
onMessageReceived()नहीं कॉल होता - इसलिए सर्वर से हमेशा data payload भेजें