Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

पुश नोटिफिकेशन मिलने पर एक्स्ट्रा नोटिफिकेशन कैसे जोड़ें?

पुश प्राप्त होने पर लोकल नोटिफिकेशन बनाना

जब 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 भेजें
  1. Android में PreferenceScreen पर बटन कैसे जोड़ें? Android में PreferenceScreen पर बटन कैसे जोड़ें?

    AndroidX Preference लाइब्रेरी का उपयोग करके सेटिंग्स स्क्रीन में कस्टम एक्शन बटन जोड़ने का तरीका। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: एक्टिविटी लेआउट res/layout/activity_main.xml - Fragment कंटेनर: <LinearLayout xmlns:android=https://schemas.android.com/apk

  1. एंड्रॉइड ऐप में कैलेंडर इवेंट कैसे जोड़ें एंड्रॉइड ऐप में कैलेंडर इवेंट कैसे जोड़ें

    इंटेंट के माध्यम से सिस्टम कैलेंडर ऐप में इवेंट जोड़ना एंड्रॉइड में कैलेंडर इवेंट जोड़ने का सबसे सरल तरीका Intent.ACTION_INSERT या Intent.ACTION_EDIT का उपयोग करके सिस्टम कैलेंडर ऐप को लॉन्च करना है। यह यूजर को इवेंट विवरण संपादित/पुष्टि करने देता है और किसी विशेष अनुमति की आवश्यकता नहीं होती। चरण

  1. Android Studio में JAR फ़ाइल को लाइब्रेरी के रूप में कैसे जोड़ें? Android Studio में JAR फ़ाइल को लाइब्रेरी के रूप में कैसे जोड़ें?

    यह गाइड बताता है कि Android Studio प्रोजेक्ट में बाहरी JAR फ़ाइल को लाइब्रेरी के रूप में कैसे जोड़ा जाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project पर जाएं और प्रोजेक्ट बनाएं। चरण 2: JAR फ़ाइल को libs फ़ोल्डर में कॉपी करें Project व्यू में app/libs फ़ोल्डर खोलें (नहीं है