I have an ongoing notification with a horizontal marquee defined by:
<?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="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="52dp"
android:layout_height="52dp"
android:background="@mipmap/ic_launcher_round"
android:contentDescription="@string/app_name" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="false"
android:singleLine="true" >
<requestFocus />
</TextView>
</LinearLayout>
</LinearLayout>
and
fun createNotificationChannel(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = "channel_name"
val descriptionText = "channel_description"
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel("CHANNEL_ID", name, importance).apply {
description = descriptionText
}
// Register the channel with the system
val notificationManager: NotificationManager =
context.applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
suspend fun showNotification(context: Context) {
var collapsedView = RemoteViews(context.packageName, R.layout.default_notification_collapsed)
collapsedView.setTextViewText(
R.id.text,
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
val builder = NotificationCompat.Builder(context.applicationContext, "CHANNEL_ID")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setCustomContentView(collapsedView)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setOnlyAlertOnce(true)
.setOngoing(true)
with(NotificationManagerCompat.from(context.applicationContext)) {
notify(1, builder.build())
}
}
This works on Oreo just fine but when a notification comes in from another app, the marquee stops on the lock screen. This seems to happen on notifications from Gmail and Twitter but not on system notifications like "open wifi detected".
Also, it doesn't seem to happen right away. When a new notification comes in, it appears above my ongoing notification (the marquee continues scrolling) and after a few seconds, android rearranges the notifications so my ongoing notification appears on top and the marquee stops.
Is this expected behaviour? How can I prevent this from happening?
from Marquee notification loses focus and stops when another notification comes in
No comments:
Post a Comment