नोटिफिकेशन के साथ टोस्ट/कस्टम अलर्ट दिखाना
यह उदाहरण नोटिफिकेशन बनाने के साथ-साथ 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()। रिच इंटरैक्शन के लिए फुल-स्क्रीन इंटेंट या डायलॉग-थीम एक्टिविटी। सिस्टम ओवरले के लिए विशेष परमिशन और सर्विस जरूरी।