Skip to content

Instantly share code, notes, and snippets.

@umangmoe
Created August 1, 2022 11:26
Show Gist options
  • Save umangmoe/039d1724d55ea7d4e418ac18c39fcca9 to your computer and use it in GitHub Desktop.
Save umangmoe/039d1724d55ea7d4e418ac18c39fcca9 to your computer and use it in GitHub Desktop.
Test Custom Views for Push Notification
<?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="wrap_content"
android:background="#1574B3"
android:orientation="vertical">
<TextView
android:id="@+id/title"
style="@style/TextAppearance.Compat.Notification.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:maxLines="1"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s" />
<TextView
android:id="@+id/message"
style="@style/TextAppearance.Compat.Notification.Line2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:maxLines="2"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." />
</LinearLayout>
package com.moengage.sampleapp
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
/**
* @author Umang Chamaria
*/
private const val CHANNEL_ID = "test_custom_view_channel"
private const val CHANNEL_NAME = "Custom Notification"
// To post a notification call CustomPushTest().testPush()
class CustomPushTest {
fun testPush(context: Context) {
setupNotificationChannel(context)
val builder = buildNotification(context)
postNotification(context, builder)
}
private fun buildNotification(context: Context): NotificationCompat.Builder {
return NotificationCompat.Builder(context, CHANNEL_ID)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
// set your own small icon
.setSmallIcon(R.drawable.small_icon)
.setCustomContentView(RemoteViews(context.packageName, R.layout.collapsed_state))
.setCustomBigContentView(RemoteViews(context.packageName, R.layout.expanded_state))
}
private fun postNotification(
context: Context,
builder: NotificationCompat.Builder,
notificationId: Int = System.currentTimeMillis().toInt()
) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as
NotificationManager
notificationManager.notify(notificationId, builder.build())
}
private fun setupNotificationChannel(context: Context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
if (doesNotificationChannelExists(context, CHANNEL_ID)) return
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel =
NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT)
manager.createNotificationChannel(channel)
}
private fun doesNotificationChannelExists(context: Context, channelId: String): Boolean {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return true
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
return manager.getNotificationChannel(channelId) != null
}
}
<?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="wrap_content"
android:background="#1574B3"
android:orientation="vertical">
<TextView
android:id="@+id/title"
style="@style/TextAppearance.Compat.Notification.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:maxLines="1"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s" />
<TextView
android:id="@+id/message"
style="@style/TextAppearance.Compat.Notification.Line2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:maxLines="10"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." />
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment