I am building an Android app using Kotlin. I am trying to show notification following this tutorial because I want to support the older versions of the Android as well, https://www.geeksforgeeks.org/notifications-in-kotlin/. But it is not showing the notification.
This is my main activity to show notification
class MainActivity : AppCompatActivity() {
lateinit var notificationManager: NotificationManager
lateinit var notificationChannel: NotificationChannel
lateinit var builder: Notification.Builder
private val channelId = "i.apps.notifications"
private val description = "Test notification"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
//seedTestData()
showNotification()
}
private fun showNotification() {
val intent = Intent(this, MessageDetailsActivity::class.java)
intent.putExtra(MessageDetailsActivity.KEY_MESSAGE, "Test message")
// FLAG_UPDATE_CURRENT specifies that if a previous
// PendingIntent already exists, then the current one
// will update it with the latest intent
// 0 is the request code, using it later with the
// same method again will get back the same pending
// intent for future reference
// intent passed here is to our afterNotification class
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
// RemoteViews are used to use the content of
// some different layout apart from the current activity layout
val contentView = RemoteViews(packageName, R.layout.activity_message_details)
// checking if android version is greater than oreo(API 26) or not
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = NotificationChannel(channelId, description, NotificationManager.IMPORTANCE_HIGH)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.GREEN
notificationChannel.enableVibration(false)
notificationManager.createNotificationChannel(notificationChannel)
builder = Notification.Builder(this, channelId)
.setContent(contentView)
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(this.resources, R.drawable.ic_launcher_background))
.setContentIntent(pendingIntent)
} else {
builder = Notification.Builder(this)
.setContent(contentView)
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(this.resources, R.drawable.ic_launcher_background))
.setContentIntent(pendingIntent)
}
notificationManager.notify(1234, builder.build())
}
}
This is my activity_message_details.xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:padding="15dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textColor="@color/primary_text"
android:id="@+id/message_details_message"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.appcompat.widget.AppCompatButton
android:layout_marginTop="10dp"
android:background="@color/accent_color"
android:id="@+id/message_details_share_button"
android:text="SHARE_MESSAGE"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>
When I run the code, it is not showing the notification. What is wrong with my code and how can I fix it?
When I've ran your code I received this error:
Bad notification posted from package com.sandbox.myapplication:
Couldn not inflate contentViewsandroid.view.InflateException:
Binary XML file line #9: Binary XML file line #9:
Error inflating class android.widget.ScrollView
and app crashed.
The problem is that you are using android.widget.ScrollView
androidx.appcompat.widget.AppCompatButton
in your layout, and this is layout for the RemoteView
. For RemoteViews
you can use only classes and widgets listed here https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
If you will remove android.widget.ScrollView
and androidx.appcompat.widget.AppCompatButton
from the layout all should work.
So you need to restruct your layout using classes and widgets from the list.