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

पुश नोटिफिकेशन मिलने पर एंड्रॉइड एक्टिविटी कैसे खोलें?

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"
  }
}

ऐप को फोरग्राउंड में रखकर टेस्ट करें, या डेटा-ओनली पुश भेजें।

  1. Android में रिमाइंडर नोटिफिकेशन कैसे बनाएं? Android में रिमाइंडर नोटिफिकेशन कैसे बनाएं?

    Android में रिमाइंडर नोटिफिकेशन बनाना यह उदाहरण दर्शाता है कि Android में रिमाइंडर नोटिफिकेशन कैसे बनाया जाता है। चरण 1 - नया प्रोजेक्ट बनाएं Android Studio में File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें। चरण 2 - res/layout/activity_main.xml में कोड जोड़ें <?

  1. एंड्रॉइड में एक्टिविटी को कैसे रीस्टार्ट करें? एंड्रॉइड में एक्टिविटी को कैसे रीस्टार्ट करें?

    एंड्रॉइड एक्टिविटी को प्रोग्रामेटिकली रीस्टार्ट करना यह गाइड एक्टिविटी को रीस्टार्ट करने के विभिन्न तरीकों को दर्शाता है, जिसमें इंटेंट फ्लैग्स और रिक्रिएशन शामिल हैं। लेआउट (activity_main.xml) <RelativeLayout xmlns:android=https://schemas.android.com/apk/res/android android:layout_width=mat

  1. Android में सॉफ्ट कीबोर्ड के खुलने और बंद होने का लिस्टनर कैसे बनाएं? Android में सॉफ्ट कीबोर्ड के खुलने और बंद होने का लिस्टनर कैसे बनाएं?

    यह उदाहरण दर्शाता है कि Android एक्टिविटी में सॉफ्ट कीबोर्ड की विजिबिलिटी चेंज को कैसे डिटेक्ट किया जाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project पर जाएं और प्रोजेक्ट बनाएं। चरण 2: लेआउट XML में कोड जोड़ें res/layout/activity_main.xml: <?xml version=1.0 encoding=utf-