如何将自定义文本视图添加到 .addaction 以在 Kotlin 中进行通知
How to add custom textview to .addaction for notification in Kotlin
我有这个通知功能,可以在特定时间向用户发送通知
private fun sendNotification(nextPray: Pray) {
createNotificationChannel(CHANNEL_ID2)
log("Send Notification")
//small view
val collapsedView = RemoteViews(
packageName,
R.layout.notification_collapsed
)
//big view
val expandedView = RemoteViews(
packageName,
R.layout.notification_expanded
)
val snoozeIntent = Intent(this, StopAlarm::class.java)
val snoozePendingIntent = PendingIntent.getBroadcast(this, 0, snoozeIntent, 0)
val notificationIntent = Intent(this, FullAzan::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0)
expandedView.setTextViewText(R.id.text_view_collapsed_2, "Muazzin - ${nextPray.moazin}");
val mBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, CHANNEL_ID2)
.setSmallIcon(R.drawable.qalby_ic)
.setContentTitle("Tap to get the full Azan")
.setContentText("Muazzin - ${nextPray.moazin}")
.setCustomContentView(collapsedView)
.setCustomBigContentView(expandedView)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.addAction(
R.drawable.ic_stop,
getString(R.string.text_stop),
snoozePendingIntent
)
.setChannelId(CHANNEL_ID2)
.setAutoCancel(true)
stopSelf()
val notification = mBuilder.build()
notificationManager = NotificationManagerCompat.from(this)
notificationManager.notify(100, notification)
//startForeground(100, notification)
log("vibrating")
vibrate()
log("Azan Audio Started")
MediaPlayerManager.getInstance(applicationContext).azan(nextPray)
}
这是我正在使用的自定义视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="114dp"
android:background="@drawable/notifiction_bg_big"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/_20sdp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="@dimen/_50sdp"
android:layout_height="@dimen/_50sdp"
android:src="@drawable/logoinnotifications"/>
</LinearLayout>
<LinearLayout
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/firstlinear"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_view_collapsed_1"
style="@style/TextAppearance.Compat.Notification.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/poppins_semibold"
android:textSize="@dimen/_15sdp"
android:text="Tap to get the full Azan"
android:textColor="@color/white" />
<TextView
android:id="@+id/text_view_collapsed_2"
android:textSize="@dimen/_12sdp"
android:fontFamily="@font/poppins"
style="@style/TextAppearance.Compat.Notification.Info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="Muazzin - Ustaz Mizi Wahid" />
<TextView
android:layout_marginTop="@dimen/_10sdp"
android:id="@+id/text_view_collapsed_3"
android:textSize="@dimen/_12sdp"
android:fontFamily="@font/poppins_bold"
android:gravity="center"
style="@style/TextAppearance.Compat.Notification.Info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="Stop Audio" />
</LinearLayout>
我想将此 ID“text_view_collapsed_3”添加到 .addaction,这样当用户单击它时,音频将从通知中停止
因为当我应用自定义视图时,当前的 .addaction 消失了
实现此目标的正确方法是什么?
解决方案:
expandedView.setOnClickPendingIntent(R.id.text_view_collapsed_3, snoozePendingIntent);
我有这个通知功能,可以在特定时间向用户发送通知
private fun sendNotification(nextPray: Pray) {
createNotificationChannel(CHANNEL_ID2)
log("Send Notification")
//small view
val collapsedView = RemoteViews(
packageName,
R.layout.notification_collapsed
)
//big view
val expandedView = RemoteViews(
packageName,
R.layout.notification_expanded
)
val snoozeIntent = Intent(this, StopAlarm::class.java)
val snoozePendingIntent = PendingIntent.getBroadcast(this, 0, snoozeIntent, 0)
val notificationIntent = Intent(this, FullAzan::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0)
expandedView.setTextViewText(R.id.text_view_collapsed_2, "Muazzin - ${nextPray.moazin}");
val mBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, CHANNEL_ID2)
.setSmallIcon(R.drawable.qalby_ic)
.setContentTitle("Tap to get the full Azan")
.setContentText("Muazzin - ${nextPray.moazin}")
.setCustomContentView(collapsedView)
.setCustomBigContentView(expandedView)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.addAction(
R.drawable.ic_stop,
getString(R.string.text_stop),
snoozePendingIntent
)
.setChannelId(CHANNEL_ID2)
.setAutoCancel(true)
stopSelf()
val notification = mBuilder.build()
notificationManager = NotificationManagerCompat.from(this)
notificationManager.notify(100, notification)
//startForeground(100, notification)
log("vibrating")
vibrate()
log("Azan Audio Started")
MediaPlayerManager.getInstance(applicationContext).azan(nextPray)
}
这是我正在使用的自定义视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="114dp"
android:background="@drawable/notifiction_bg_big"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/_20sdp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="@dimen/_50sdp"
android:layout_height="@dimen/_50sdp"
android:src="@drawable/logoinnotifications"/>
</LinearLayout>
<LinearLayout
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/firstlinear"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_view_collapsed_1"
style="@style/TextAppearance.Compat.Notification.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/poppins_semibold"
android:textSize="@dimen/_15sdp"
android:text="Tap to get the full Azan"
android:textColor="@color/white" />
<TextView
android:id="@+id/text_view_collapsed_2"
android:textSize="@dimen/_12sdp"
android:fontFamily="@font/poppins"
style="@style/TextAppearance.Compat.Notification.Info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="Muazzin - Ustaz Mizi Wahid" />
<TextView
android:layout_marginTop="@dimen/_10sdp"
android:id="@+id/text_view_collapsed_3"
android:textSize="@dimen/_12sdp"
android:fontFamily="@font/poppins_bold"
android:gravity="center"
style="@style/TextAppearance.Compat.Notification.Info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="Stop Audio" />
</LinearLayout>
我想将此 ID“text_view_collapsed_3”添加到 .addaction,这样当用户单击它时,音频将从通知中停止
因为当我应用自定义视图时,当前的 .addaction 消失了
实现此目标的正确方法是什么?
解决方案:
expandedView.setOnClickPendingIntent(R.id.text_view_collapsed_3, snoozePendingIntent);