Kotlin android 应用未显示通知
Kotlin android app is not showing notification
我正在使用 Kotlin 构建一个 Android 应用程序。我试图在本教程之后显示通知,因为我也想支持 Android 的旧版本 https://www.geeksforgeeks.org/notifications-in-kotlin/。但是它没有显示通知。
这是我主要activity显示通知
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())
}
}
这是我的 activity_message_details.xml 文件。
<?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>
当我 运行 代码时,它没有显示通知。我的代码有什么问题,我该如何解决?
当我 运行 你的代码时,我收到了这个错误:
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
应用崩溃了。
问题是您在布局中使用 android.widget.ScrollView
androidx.appcompat.widget.AppCompatButton
,而这是 RemoteView
的布局。对于 RemoteViews
,您只能使用 类 和此处列出的小部件 https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
如果您将从布局中删除 android.widget.ScrollView
和 androidx.appcompat.widget.AppCompatButton
,一切都应该有效。
因此您需要使用 类 和列表中的小部件来重构您的布局。
我正在使用 Kotlin 构建一个 Android 应用程序。我试图在本教程之后显示通知,因为我也想支持 Android 的旧版本 https://www.geeksforgeeks.org/notifications-in-kotlin/。但是它没有显示通知。
这是我主要activity显示通知
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())
}
}
这是我的 activity_message_details.xml 文件。
<?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>
当我 运行 代码时,它没有显示通知。我的代码有什么问题,我该如何解决?
当我 运行 你的代码时,我收到了这个错误:
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
应用崩溃了。
问题是您在布局中使用 android.widget.ScrollView
androidx.appcompat.widget.AppCompatButton
,而这是 RemoteView
的布局。对于 RemoteViews
,您只能使用 类 和此处列出的小部件 https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
如果您将从布局中删除 android.widget.ScrollView
和 androidx.appcompat.widget.AppCompatButton
,一切都应该有效。
因此您需要使用 类 和列表中的小部件来重构您的布局。