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

नोटिफिकेशन प्राप्त होने पर कस्टम अलर्ट/व्यू कैसे दिखाएं?

नोटिफिकेशन के साथ टोस्ट/कस्टम अलर्ट दिखाना

यह उदाहरण नोटिफिकेशन बनाने के साथ-साथ Toast दिखाता है। वास्तविक "कस्टम अलर्ट" (डायलॉग, फुल-स्क्रीन इंटेंट) के लिए अलग तरीके हैं।

चरण 1: प्रोजेक्ट और लेआउट

सामान्य बटन वाला लेआउट।

चरण 2: 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());

        // नोटिफिकेशन के साथ टोस्ट दिखाएं
        Toast.makeText(MainActivity.this, "Custom Alert View", Toast.LENGTH_SHORT).show();
    }
}

चरण 3: मैनिफेस्ट

मानक।

वास्तविक "कस्टम अलर्ट" के तरीके

1. फुल-स्क्रीन इंटेंट (कॉल, अलार्म जैसा)

Intent fullScreenIntent = new Intent(this, CallActivity.class);
PendingIntent fullScreenPending = PendingIntent.getActivity(this, 0, fullScreenIntent, PendingIntent.FLAG_IMMUTABLE);

mBuilder.setFullScreenIntent(fullScreenPending, true); // high priority चैनल चाहिए
// मैनिफेस्ट में: 

2. एक्टिविटी को डायलॉग की तरह दिखाएं

मैनिफेस्ट में एक्टिविटी थीम सेट करें:

<activity
    android:name=".AlertActivity"
    android:theme="@style/Theme.AppCompat.Dialog"
    android:excludeFromRecents="true" />

फिर नोटिफिकेशन के setContentIntent() में उस एक्टिविटी का PendingIntent दें।

3. सिस्टम अलर्ट विंडो (ड्रॉ ओवर अदर ऐप्स)

SYSTEM_ALERT_WINDOW परमिशन चाहिए (यूजर ग्रांट करता है)। WindowManager.addView() से व्यू जोड़ें। नोटिफिकेशन से सीधे ट्रिगर नहीं कर सकते — सर्विस से करें।

4. Bubbles (API 30+)

चैट ऐप्स के लिए फ्लोटिंग बबल — setBubbleMetadata() उपयोग करें।

निष्कर्ष

सिंपल टोस्ट के लिए Toast.makeText()। रिच इंटरैक्शन के लिए फुल-स्क्रीन इंटेंट या डायलॉग-थीम एक्टिविटी। सिस्टम ओवरले के लिए विशेष परमिशन और सर्विस जरूरी।

  1. Android में कस्टम लिस्ट व्यू में सर्च फंक्शनैलिटी कैसे जोड़ें? Android में कस्टम लिस्ट व्यू में सर्च फंक्शनैलिटी कैसे जोड़ें?

    Android में कस्टम लिस्ट व्यू में सर्च फीचर इम्प्लीमेंट करना यह ट्यूटोरियल दिखाता है कि कैसे एक कस्टम लिस्ट व्यू में सर्च फंक्शनैलिटी जोड़ी जाती है, जिससे यूजर लिस्ट आइटम्स को रियल-टाइम में फ़िल्टर कर सकते हैं। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाए

  1. Android में कस्टम AlertDialog View कैसे इम्प्लीमेंट करें? Android में कस्टम AlertDialog View कैसे इम्प्लीमेंट करें?

    Android में कस्टम AlertDialog इम्प्लीमेंट करना यह ट्यूटोरियल दिखाता है कि कैसे कस्टम लेआउट के साथ AlertDialog बनाया जाता है, जिसमें कस्टम टाइटल, आइकन, मैसेज, और बटन स्टाइलिंग शामिल है। चरण 1: प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: मेन लेआउट में बटन res/layout/activity_main.

  1. Android पर नोटिफिकेशन हिस्ट्री कैसे देखें Android पर नोटिफिकेशन हिस्ट्री कैसे देखें

    हम में से अधिकांश को दैनिक आधार पर अपने फ़ोन पर अनगिनत नोटिफिकेशन प्राप्त होते हैं। लेकिन कितनी बार आपने उन्हें बहुत जल्दी स्वाइप कर दिया, और क्योंकि आप वास्तव में नहीं देख रहे थे, आपने कुछ महत्वपूर्ण खारिज कर दिया? अच्छी खबर यह है कि यदि आवश्यकता हो तो अपने नोटिफिकेशन को पुनः प्राप्त करना संभव है।