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

एंड्रॉइड नोटिफिकेशन में वाइब्रेशन और लाइट कैसे इनेबल करें?

नोटिफिकेशन में वाइब्रेशन और LED लाइट जोड़ना

नोटिफिकेशन चैनल (API 26+) में setVibrationPattern(), enableVibration(), setLightColor() से वाइब्रेशन और नोटिफिकेशन लाइट कॉन्फिगर करें।

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

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

चरण 2: कस्टम नोटिफिकेशन लेआउट

res/layout/custom_notification_layout.xml — इमेज, टाइटल, एडिटटेक्स्ट के साथ।

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="96dp"
    android:padding="10dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_marginEnd="10dp"
        android:contentDescription="@string/app_name"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/image"
        android:text="Testing"
        android:textColor="#000"
        android:textSize="18sp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_marginTop="8dp"
        android:layout_toEndOf="@+id/image"
        android:hint="Enter something..."
        android:inputType="text"
        android:textSize="14sp" />
</RelativeLayout>

चरण 3: 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.RemoteViews;

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) {
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this, default_notification_channel_id);

        mBuilder.setContent(contentView);
        mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
        mBuilder.setAutoCancel(true);
        mBuilder.setVisibility(NotificationCompat.VISIBILITY_SECRET);

        // वाइब्रेशन पैटर्न: {देरी, वाइब्रेट, देरी, वाइब्रेट...} मिलीसेकंड में
        long[] VIBRATE_PATTERN = {0, 500, 200, 300};

        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);
            
            // LED लाइट रंग सेट करें (डिवाइस सपोर्ट करे तो)
            notificationChannel.setLightColor(getResources().getColor(R.color.colorAccent));
            
            // वाइब्रेशन पैटर्न और इनेबल
            notificationChannel.setVibrationPattern(VIBRATE_PATTERN);
            notificationChannel.enableVibration(true);
            
            mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
            assert mNotificationManager != null;
            mNotificationManager.createNotificationChannel(notificationChannel);
        }

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

चरण 4: मैनिफेस्ट — वाइब्रेट परमिशन जरूरी

<uses-permission android:name="android.permission.VIBRATE" />

वाइब्रेशन पैटर्न समझें

long[] pattern = {delay, vibrate, delay, vibrate, ...}

  • {0, 500} — तुरंत 500ms वाइब्रेट
  • {0, 500, 200, 300} — 500ms वाइब्रेट, 200ms रुकें, 300ms वाइब्रेट
  • {0, 200, 100, 200, 100, 200} — तीन छोटे वाइब्रेशन

LED लाइट के बारे में

  • सभी डिवाइस में नोटिफिकेशन LED नहीं होता
  • setLightColor() केवल LED वाले डिवाइस पर काम करता है
  • रंग: Color.RED, Color.GREEN, Color.BLUE, या कस्टम ARGB
  • ब्लिंक रेट सिस्टम कंट्रोल करता है

नोटिफिकेशन बिल्डर पर सीधे सेट करना (पुराना API)

API 26 से पहले:

mBuilder.setVibrate(new long[]{0, 500, 200, 300});
mBuilder.setLights(Color.RED, 500, 500); // रंग, ऑन-टाइम, ऑफ-टाइम

लेकिन API 26+ में चैनल पर सेट करना अनिवार्य है — बिल्डर पर सेट करने का कोई असर नहीं होता।

  1. Nexus 6P और 5X को रूट कैसे करें Nexus 6P और 5X को रूट कैसे करें

    Nexus फोन हमेशा Android उत्साही लोगों के बीच एक लोकप्रिय विकल्प रहे हैं क्योंकि उन्हें कितनी आसानी से रूट और मोड किया जा सकता है। यह Nexus 6P और Nexus 5X के लिए अभी भी सच है, खासकर प्रसिद्ध डेवलपर Chainfire की एक नई सिस्टमलेस रूट प्रक्रिया के लिए धन्यवाद। सिस्टमलेस रूट क्लीनर और कम आक्रामक है, अनरू

  1. नवीनतम एंड्रॉइड 9 और 10 अपडेट के लिए UI/UX कैसे डिज़ाइन करें नवीनतम एंड्रॉइड 9 और 10 अपडेट के लिए UI/UX कैसे डिज़ाइन करें

    Google ने पिछले सितंबर में नवीनतम एंड्रॉइड 10 जारी किया, और हालांकि यह केवल कुछ नवीनतम, सबसे प्रीमियम फोन पर उपलब्ध है, बहुत सारे फोन निर्माताओं से निकट भविष्य में अपने कुछ सबसे हालिया फोन पर एंड्रॉइड 10 रोल आउट करने की उम्मीद है।समान पढ़ें: ग्राफिक्स डिज़ाइन के लिए सर्वश्रेष्ठ कंप्यूटरएंड्रॉइड 10 ए

  1. एंड्रॉइड पर पुश नोटिफिकेशन इनेबल करें: स्टेप-बाय-स्टेप गाइड एंड्रॉइड पर पुश नोटिफिकेशन इनेबल करें: स्टेप-बाय-स्टेप गाइड

    पुश नोटिफिकेशन एक संदेश है जो समय-समय पर आपके स्मार्टफोन पर पॉप अप होता है। अगर आप ऑनलाइन शॉपिंग करना पसंद करते हैं, तो पुश नोटिफिकेशन आपके लिए बहुत सारे ऑफर लाते हैं; उनमें से कुछ केवल कुछ घंटों के लिए उपलब्ध हो सकते हैं। अगर आप गलती से एंड्रॉइड पर पुश नोटिफिकेशन अक्षम कर देते हैं, तो इसे फिर से इन