Reputation: 2045
I spend the whole day to figure out why my custom notifications are not being displayed. At the end, it was the problem with constraint layout. When I try to build custom_notification.xml with constraint layout the notification does not show up but if I change to linear layout the notification works. Can anybody tell me why this is happening?
custom_notitication.xml with linear layout (This layout works)
<LinearLayout 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:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Button"
<Button
android:layout_weight="1"
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Button"
/>
</LinearLayout>
custom_notitication.xml with Constraint Layout(this is not working)
<android.support.constraint.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:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:layout_weight="1"
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button"
/>
</android.support.constraint.ConstraintLayout>
and kotlin code
fun getNotification(context: Context): Notification {
val mNotificationManager1: NotificationManager?
val notification: Notification
mNotificationManager1 = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
createChannel(mNotificationManager1)
val contentView = RemoteViews(applicationContext.packageName, R.layout.custom_notifaction)
val mBuilder = NotificationCompat.Builder(applicationContext,"LockScreenTouch")
.setSmallIcon(R.mipmap.ic_launcher)
.setCustomBigContentView(contentView)
notification = mBuilder.build()
mNotificationManager1.notify(PostNotifactionActivity.ONGOING_NOTIFICATION_ID, notification)
return notification
}
@TargetApi(26)
@Synchronized
private fun createChannel(notificationManager: NotificationManager?) {
val name = "lockScreen"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val mChannel = NotificationChannel("LockScreenTouch", name, importance)
mChannel.enableLights(true)
mChannel.lightColor = Color.BLUE
notificationManager!!.createNotificationChannel(mChannel)
}
and my buid.gradle
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
Upvotes: 3
Views: 1503
Reputation: 17854
Notification layouts use the RemoteViews implementation—the same as widgets.
There are very few Views that are compatible with RemoteViews. It's only a subset of the native/framework Views (ie no support Views, no library Views, no custom Views), and that subset isn't exhaustive:
No other Views will work.
https://developer.android.com/reference/android/widget/RemoteViews
Upvotes: 13