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

एंड्रॉइड में नोटिफिकेशन पर कस्टम साउंड कैसे बजाएं?

नोटिफिकेशन के लिए कस्टम रिंगटोन चुनना और बजाना

यूजर को रिंगटोन पिकर से साउंड चुनने दें, फिर नोटिफिकेशन में उस URI को सेट करें। RingtoneManager.ACTION_RINGTONE_PICKER इंटेंट उपयोग करें।

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:onClick="setRingtone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:text="Set Ringtone" />

    <Button
        android:onClick="createNotification"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="16dp"
        android:text="Create Notification" />
</RelativeLayout>

चरण 2: MainActivity — रिंगटोन पिकर + नोटिफिकेशन

package app.tutorialspoint.com.notifyme;

import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    public static final String NOTIFICATION_CHANNEL_ID = "10001";
    private final static String default_notification_channel_id = "default";

    String chosenRingtone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onNewIntent(getIntent());
    }

    // रिंगटोन पिकर खोलें
    public void setRingtone(View view) {
        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
        this.startActivityForResult(intent, 5);
    }

    // नोटिफिकेशन बनाएं — चुनी हुई रिंगटोन के साथ
    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 (chosenRingtone != null) {
            mBuilder.setSound(Uri.parse(chosenRingtone));
        }

        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);
            // चैनल पर भी डिफॉल्ट साउंड सेट कर सकते हैं
            if (chosenRingtone != null) {
                notificationChannel.setSound(Uri.parse(chosenRingtone), null);
            }
            mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
            assert mNotificationManager != null;
            mNotificationManager.createNotificationChannel(notificationChannel);
        }

        assert mNotificationManager != null;
        mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == 5) {
            Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            if (uri != null) {
                this.chosenRingtone = uri.toString();
            } else {
                this.chosenRingtone = null;
            }
        }
    }
}

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

मानक।

महत्वपूर्ण बिंदु

  1. बिल्डर पर setSound() — API 26 से पहले काम करता है
  2. चैनल पर setSound() — API 26+ के लिए अनिवार्य (बिल्डर पर सेट करने का असर नहीं)
  3. चैनल बनने के बाद उसका साउंड बदल नहीं सकते — नया चैनल बनाना पड़ता है
  4. रिंगटोन URI को SharedPreferences में सेव करें ताकि ऐप रीस्टार्ट पर याद रहे

रॉ फाइल से साउंड (ऐप में बंडल)

// res/raw/notification_sound.mp3 रखें
Uri soundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification_sound);
mBuilder.setSound(soundUri);

साइलेंट नोटिफिकेशन

mBuilder.setSound(null); // या चैनल पर setSound(null, null)

ऐप चलाएं, "Set Ringtone" दबाकर साउंड चुनें, फिर "Create Notification" — चुना हुआ साउंड बजेगा।

  1. Android में कस्टम नोटिफिकेशन लेआउट और टेक्स्ट कलर कैसे बनाएं? Android में कस्टम नोटिफिकेशन लेआउट और टेक्स्ट कलर कैसे बनाएं?

    Android में कस्टम नोटिफिकेशन लेआउट बनाने का पूरा गाइड यह उदाहरण RemoteViews और NotificationCompat.Builder का उपयोग करके पूरी तरह से कस्टमाइज्ड नोटिफिकेशन (कस्टम लेआउट, इमेज, टेक्स्ट कलर, बटन) बनाने का तरीका दिखाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: म

  1. विंडोज 10 में कस्टम नोटिफिकेशन साउंड कैसे सेट करें विंडोज 10 में कस्टम नोटिफिकेशन साउंड कैसे सेट करें

    माइक्रोसॉफ्ट ने विंडोज 10 में नोटिफिकेशन ध्वनियों को फिर से कल्पना की। जब कोई टोस्ट नोटिफिकेशन आपके पीसी पर आता है, तो एक डिफ़ॉल्ट नोटिफिकेशन साउंड बजती है ताकि आपको अलर्ट की उपस्थिति के बारे में पता चले। हालांकि, कभी-कभी उपयोगकर्ता डिफ़ॉल्ट चाइम से असहज हो सकते हैं और अपना खुद का परीक्षण करना चाहते

  1. Android पर Snapchat नोटिफिकेशन साउंड बदलें: चरण-दर-चरण गाइड Android पर Snapchat नोटिफिकेशन साउंड बदलें: चरण-दर-चरण गाइड

    Snapchat Gen Z के बीच एक लोकप्रिय और मांग वाला एप्लिकेशन है। कैमरा, तस्वीरों के लिए फिल्टर, वॉयस कॉलिंग, वीडियो कॉलिंग और टेक्स्टिंग जैसी सुविधाओं के साथ, यह उपयोगकर्ताओं के लिए एक ऑल-इन-वन ऐप बन गया है। इतना ही नहीं, यह अपने उपयोगकर्ताओं को एक Streak के माध्यम से अपने दोस्तों और अन्य स्नैप चैटर्स क