Home > Enterprise >  android notification not working in kotlin language
android notification not working in kotlin language

Time:04-10

enter image description here

Hi all i am new to kotlin and android platform. I wish to send notification when i click on button "click her for notification" but it not working when i click on button getting message shown in image. I tried .apk file on real device but still same problem. I am using API is 31. MainActivity.kt

package com.example.firstapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat

class MainActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?) 
{
        val CHANNEL_ID= "normal data"
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val notifyy = findViewById<Button>(R.id.note);
        notifyy.setOnClickListener {
            //Toast.makeText(applicationContext,"this is S!",Toast.LENGTH_SHORT).show()
            var builder = NotificationCompat.Builder(this, CHANNEL_ID)
            builder.setSmallIcon(R.drawable.notification_icon)
            builder .setContentTitle("My notification")
            builder.setContentText("Much longer text that cannot fit one line...")
            builder.setStyle(NotificationCompat.BigTextStyle().bigText("Much longer text that cannot fit one line"))
            builder.setPriority(NotificationCompat.PRIORITY_DEFAULT)
            with(NotificationManagerCompat.from(this)) {
                // notificationId is a unique int for each notification that you must define
                val notificationId:Int=11
                notify(notificationId, builder.build())
            }

        }


    }
}

ActivityMain.xml

<?xml version="1.0" encoding="utf-8"?>`   `
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@ id/textView"
        android:layout_width="127dp"
        android:layout_height="86dp"
        android:text="hellow sharad"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textColor="#E91E63"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.32"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.137" />

    <Button
        android:id="@ id/note"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click here for notification"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.687"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.669" />

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

Android Notifications

Check out this repo of mine. I made it specifically for creating notifications in android.

CodePudding user response:

You have to first create the Notification channel then only notification posted on the channel(in your case channel is val CHANNEL_ID = "normal data") will be shown

val chanel = NotificationChannel(CHANNEL_ID, name /* name of your channel */, importance).apply {
    description = /* description text of the channel */
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    // Notification channnel is only supported above O

    // Register the channel with the system
    val appContext = /* your application context */    
    val notificationManager: NotificationManager = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.createNotificationChannels(listOf(chanel))
}
  • Related