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

Kotlin का उपयोग करके एंड्रॉइड ऐप लॉन्चर में नोटिफिकेशन काउंट कैसे प्रदर्शित करें?

परिचय

यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके एंड्रॉइड ऐप लॉन्चर आइकन पर नोटिफिकेशन बैज काउंट कैसे प्रदर्शित किया जाए।

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

Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।

चरण 2: लेआउट XML कॉन्फ़िगर करें

res/layout/activity_main.xml में निम्नलिखित कोड जोड़ें:

<?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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Tutorials Point"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="32sp"
        android:textStyle="bold" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:onClick="createNotification"
        android:text="नोटिफिकेशन बनाएं" />

</RelativeLayout>

चरण 3: Kotlin कोड लागू करें

src/MainActivity.kt में निम्नलिखित कोड जोड़ें:

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat

class MainActivity : AppCompatActivity() {
    private var count = 0
    private val channelId = "10001"
    private val defaultChannelId = "default"

    override fun onResume() {
        super.onResume()
        count = 0 // ऐप खुलने पर काउंट रीसेट
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
    }

    fun createNotification(view: View) {
        count++
        
        val notificationIntent = Intent(applicationContext, MainActivity::class.java).apply {
            putExtra("fromNotification", true)
            flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
        }
        
        val pendingIntent = PendingIntent.getActivity(
            this, 0, notificationIntent, 
            PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
        )
        
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        
        val builder = NotificationCompat.Builder(applicationContext, defaultChannelId).apply {
            setContentTitle("मेरा नोटिफिकेशन")
            setContentIntent(pendingIntent)
            setContentText("नोटिफिकेशन लिसनर सर्विस उदाहरण")
            setSmallIcon(R.drawable.ic_launcher_foreground)
            setAutoCancel(true)
            setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
            setNumber(count)
        }
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val importance = NotificationManager.IMPORTANCE_HIGH
            val notificationChannel = NotificationChannel(
                channelId, "नोटिफिकेशन चैनल नाम", importance
            )
            builder.setChannelId(channelId)
            notificationManager.createNotificationChannel(notificationChannel)
        }
        
        notificationManager.notify(System.currentTimeMillis().toInt(), builder.build())
    }
}

चरण 4: मैनिफेस्ट कॉन्फ़िगर करें

androidManifest.xml में निम्नलिखित कोड जोड़ें:

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

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

    <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>

ऐप चलाएं

अपने एंड्रॉइड डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप का डिफॉल्ट स्क्रीन देखें।

महत्वपूर्ण नोट्स

  • नोटिफिकेशन बैज (बैज काउंट) लॉन्चर पर निर्भर करता है - सभी लॉन्चर इसे सपोर्ट नहीं करते
  • एंड्रॉइड 8+ (API 26+) पर नोटिफिकेशन चैनल अनिवार्य है
  • कुछ डिवाइसेज पर शॉर्टकट बैज के लिए अतिरिक्त लाइब्रेरी (जैसे ShortcutBadger) की आवश्यकता हो सकती है
  1. Android ऐप लॉन्चर आइकन पर नोटिफिकेशन काउंट कैसे डिस्प्ले करें? Android ऐप लॉन्चर आइकन पर नोटिफिकेशन काउंट कैसे डिस्प्ले करें?

    Android ऐप लॉन्चर आइकन पर नोटिफिकेशन काउंट डिस्प्ले करना यह उदाहरण दर्शाता है कि Android ऐप लॉन्चर आइकन पर नोटिफिकेशन काउंट कैसे डिस्प्ले किया जाता है। चरण 1 - नया प्रोजेक्ट बनाएं Android Studio में File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें। चरण 2 - res/layout/

  1. एंड्रॉइड में टोस्ट कैसे प्रदर्शित करें? एंड्रॉइड में टोस्ट कैसे प्रदर्शित करें?

    एंड्रॉइड में Toast संदेश दिखाने का बुनियादी उदाहरण यह उदाहरण दर्शाता है कि Toast.makeText() का उपयोग करके सरल टोस्ट संदेश कैसे प्रदर्शित करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट (ConstraintLayout) res/layout/activity_main.xml: <?xml version=1.0

  1. Android ऐप में नोटिफिकेशन काउंट कैसे दिखाएं? Android ऐप में नोटिफिकेशन काउंट कैसे दिखाएं?

    Android ऐप लॉन्चर आइकन पर नोटिफिकेशन बैज/काउंट दिखाना यह ट्यूटोरियल दिखाता है कि कैसे नोटिफिकेशन भेजते समय बैज काउंट सेट किया जाता है, जिससे लॉन्चर आइकन पर अनरीड काउंट दिखाई देता है। चरण 1: प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट res/layout/activity_main.xml: <?xml