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

एंड्रॉइड NotificationBuilder उदाहरण

NotificationCompat.Builder का बेसिक उदाहरण

यह आर्टिकल NotificationCompat.Builder से सिंपल नोटिफिकेशन बनाने का न्यूनतम कोड दिखाता है।

चरण 1: नया प्रोजेक्ट

Android Studio में File → New Project।

चरण 2: लेआउट

<?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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="16dp"
        android:onClick="createNotification"
        android:text="Create Notification" />
</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;

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);
    }

    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());
    }
}

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

<?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>
    </application>
</manifest>

NotificationCompat.Builder के मुख्य मेथड

मेथडविवरण
setContentTitle()मुख्य शीर्षक
setContentText()मुख्य टेक्स्ट
setSmallIcon()स्टेटस बार आइकन (अनिवार्य)
setLargeIcon()बड़ा आइकन (दाईं ओर)
setAutoCancel(true)टैप पर खुद हट जाए
setContentIntent()टैप पर खुलने वाला इंटेंट
setStyle()BigTextStyle, InboxStyle, BigPictureStyle
setChannelId()API 26+ के लिए चैनल ID
addAction()एक्शन बटन जोड़ें
setPriority()प्राथमिकता (HIGH, DEFAULT, LOW)

यह बेसिक टेम्पलेट है — इसे अपनी जरूरत के अनुसार बढ़ाएं।

  1. Android Shared Preferences में apply() का उपयोग कैसे करें? उदाहरण सहित Android Shared Preferences में apply() का उपयोग कैसे करें? उदाहरण सहित

    Android Shared Preferences में apply() मेथड का उपयोग Shared Preferences apply() में जाने से पहले, हमें पता होना चाहिए कि Android में Shared Preferences क्या है। Shared Preferences का उपयोग करके, हम मानों को कुंजी और मान जोड़ी के रूप में संग्रहीत या पुनर्प्राप्त कर सकते हैं। Shared Preferences में पा

  1. Android में ToggleButton का उदाहरण Android में ToggleButton का उदाहरण

    Android ToggleButton - ऑन/ऑफ स्टेट्स के साथ बटन ToggleButton एक दो-स्थिति वाला बटन है (चेक्ड/अनचेक्ड) जो CompoundButton से विरासत में मिला है। ऑन/ऑफ सेटिंग्स के लिए आदर्श। चरण 1: प्रोजेक्ट बनाएं Android Studio: File → New Project। चरण 2: लेआउट (activity_main.xml) <?xml version=1.0 encoding=utf-

  1. Android Studio में फ्रैगमेंट ट्यूटोरियल उदाहरण के साथ Android Studio में फ्रैगमेंट ट्यूटोरियल उदाहरण के साथ

    Android में फ्रैगमेंट ट्यूटोरियल यह उदाहरण Android Studio में फ्रैगमेंट ट्यूटोरियल को उदाहरण के साथ दर्शाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं। चरण 2: res/layout/activity_main.xml में कोड जोड़ें <?xml version=