एक साथ कई नोटिफिकेशन प्रदर्शित करना
प्रत्येक नोटिफिकेशन के लिए यूनिक ID का उपयोग करके कई नोटिफिकेशन एक साथ दिखाए जा सकते हैं। यहां साउंड, वाइब्रेशन, लाइट के साथ उदाहरण दिया गया है।
चरण 1-2: प्रोजेक्ट और लेआउट (मानक)
चरण 3: res/raw फोल्डर में साउंड फाइल जोड़ें
res/raw/quite_impressed.mp3 जैसी साउंड फाइल रखें।
चरण 4: MainActivity.java
package app.tutorialspoint.com.notifyme;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public static final String NOTIFICATION_CHANNEL_ID = "10001";
private static final String DEFAULT_CHANNEL_ID = "default";
private int notificationCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnCreateNotification = findViewById(R.id.btnCreateNotification);
btnCreateNotification.setOnClickListener(v -> {
notificationCount++;
// कस्टम साउंड URI
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
+ getPackageName() + "/raw/quite_impressed.mp3");
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("नोटिफिकेशन #" + notificationCount)
.setSound(sound)
.setContentText("यह नोटिफिकेशन #" + notificationCount + " है")
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID, "चैनल नाम", importance);
channel.enableLights(true);
channel.setLightColor(Color.RED);
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
channel.setSound(sound, audioAttributes);
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
assert manager != null;
manager.createNotificationChannel(channel);
}
assert manager != null;
// यूनिक ID से मल्टीपल नोटिफिकेशन
manager.notify((int) System.currentTimeMillis(), builder.build());
});
}
}
चरण 5: AndroidManifest.xml
मल्टीपल नोटिफिकेशन के लिए टिप्स
- यूनिक ID -
System.currentTimeMillis()या इन्क्रीमेंटिंग काउंटर उपयोग करें। - अपडेट करना - समान ID के साथ
notify()कॉल करने पर नोटिफिकेशन अपडेट होता है। - हटाना -
manager.cancel(id)याmanager.cancelAll()। - चैनल प्रति ऐप - एक ऐप में कई चैनल बना सकते हैं (अलग-अलग महत्व/सेटिंग्स के लिए)।