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

एंड्रॉइड नोटिफिकेशन उदाहरण: वाइब्रेशन, साउंड, एक्शन और बिग व्यू स्टाइल्स

उन्नत नोटिफिकेशन फीचर्स

एक ही नोटिफिकेशन में वाइब्रेशन, कस्टम साउंड, एक्शन बटन, और एक्सपैंडेड बिग टेक्स्ट स्टाइल कैसे जोड़ें।

चरण 1-2: प्रोजेक्ट और लेआउट (ConstraintLayout में बटन)

चरण 3: MainActivity.java

package app.tutorialspoint.com.notifyme;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
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 {
    private static final String DEFAULT_CHANNEL_ID = "default";

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

        Button btnCreateNotification = findViewById(R.id.btnCreateNotification);
        btnCreateNotification.setOnClickListener(v -> {
            // टैप पर खुलने वाला इंटेंट
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            PendingIntent resultPendingIntent = PendingIntent.getActivity(
                MainActivity.this, 0, intent, 
                PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE
            );

            // एक्शन बटन के लिए अलग इंटेंट
            Intent actionIntent = new Intent(getApplicationContext(), MainActivity.class);
            actionIntent.setAction("ACTION_ADD");
            PendingIntent actionPendingIntent = PendingIntent.getActivity(
                MainActivity.this, 1, actionIntent,
                PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE
            );

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("उन्नत नोटिफिकेशन")
                .setContentText("वाइब्रेशन, साउंड, एक्शन और बिग व्यू के साथ")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(
                    "बिग व्यू स्टाइल: यह लंबा टेक्स्ट है जो एक्सपैंड होने पर पूरी तरह दिखता है। " +
                    "Android 7.0+ में BigTextStyle, BigPictureStyle, InboxStyle, MessagingStyle उपलब्ध हैं।"))
                .setContentIntent(resultPendingIntent)
                .addAction(R.drawable.ic_launcher_foreground, "जोड़ें", actionPendingIntent)
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}) // वाइब्रेशन पैटर्न
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setAutoCancel(true);

            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            manager.notify((int) System.currentTimeMillis(), builder.build());
        });
    }
}

चरण 4: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.tutorialspoint.com.notifyme">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    </application>
</manifest>

नोटिफिकेशन स्टाइल्स तुलना

स्टाइलउपयोगमुख्य मेथड
BigTextStyleलंबा टेक्स्ट.bigText(String)
BigPictureStyleबड़ी इमेज.bigPicture(Bitmap)
InboxStyleसूची/संदेश.addLine(CharSequence)
MessagingStyleचैट UI.addMessage(Message)
MediaStyleमीडिया कंट्रोल.setMediaSession(token)

एक्शन बटन

  • .addAction(icon, title, pendingIntent) - नोटिफिकेशन में बटन जोड़ता है
  • Android 7.0+ में एक्शन डायरेक्ट रिप्लाई (RemoteInput) भी सपोर्ट करते हैं
  • अधिकतम 3 एक्शन दिखते हैं (कॉम्पैक्ट व्यू में)

वाइब्रेशन पैटर्न

long[]{delay, vibrate, delay, vibrate...} - पहला मान प्रारंभिक देरी है।

  1. Android में ड्रैग एंड ड्रॉप कैसे उपयोग करें? Android में ड्रैग एंड ड्रॉप कैसे उपयोग करें?

    Android Drag and Drop API से व्यू मूव करना यह उदाहरण दर्शाता है कि View.OnDragListener और View.OnTouchListener का उपयोग करके दो कंटेनर्स के बीच एक इमेज को ड्रैग-एंड-ड्रॉप कैसे किया जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट फ़ाइल (res/layout/activity_

  1. इन 11 ऐप्स और ट्रिक्स से अपने Android नोटिफिकेशन को मास्टर करें इन 11 ऐप्स और ट्रिक्स से अपने Android नोटिफिकेशन को मास्टर करें

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

  1. इन ऐप्स से 10 बड़ी Android समस्याओं और झुंझलाहट को हल करें इन ऐप्स से 10 बड़ी Android समस्याओं और झुंझलाहट को हल करें

    हर कुछ महीनों में, एक नया फ्लैगशिप स्मार्टफोन टॉप-ऑफ-द-लाइन प्रोसेसर और जितना RAM आप जानते हैं कि क्या करना है, उससे अधिक के साथ लॉन्च होता है। लेकिन फिर भी, कुछ समय बाद, फ्लैगशिप फोन अभी भी लैग करना शुरू कर देते हैं। वास्तव में, यहां तक कि उच्चतम-स्तरीय Android स्मार्टफोन की भी अपनी झुंझलाहट होती ह