1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] How to customize font family of the text in custom notification...

Discussão em 'Mobile' iniciado por Stack, Outubro 10, 2024 às 03:12.

  1. Stack

    Stack Membro Participativo

    How to use custom fonts for text in notifications in android, i have tried a way of creating folder "font" and the add font.ttf file to "font/file_name.ttf" but its not working. I want to change by default text font in notification.enter image description here

    MainActivity.kt


    class MainActivity: FlutterActivity() {
    private val CHANNEL = "flutter.native/helper"
    private lateinit var mediaSession: MediaSessionCompat

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
    super.configureFlutterEngine(flutterEngine)
    GeneratedPluginRegistrant.registerWith(flutterEngine)

    mediaSession = MediaSessionCompat(this, "MediaSession")

    MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
    when (call.method) {
    "showNotificationFromNative" -> {
    val title = call.argument<String>("title") ?: "Empty"
    val message = call.argument<String>("message") ?: "Empty"
    val greetings = showSimpleNotification(title, message)
    result.success(greetings)
    }
    "showCustomNotificationFromNative" -> {
    val args = call.arguments as? Map<String, Any>
    if (args != null) {
    val greetings = showCustomNotification(args)
    result.success(greetings)
    } else {
    result.error("INVALID_ARGUMENTS", "Arguments must be a Map<String, Any>", null)
    }
    }
    "showMediaNotification" -> {
    val title = call.argument<String>("title") ?: ""
    val artist = call.argument<String>("artist") ?: ""
    val album = call.argument<String>("album") ?: ""
    val duration = call.argument<Int>("duration") ?: 0
    val position = call.argument<Int>("position") ?: 0
    showMediaNotification(title, artist, album, duration, position)
    result.success(null)
    }
    else -> {
    result.notImplemented()
    }
    }
    }
    }





    private fun showCustomNotification(args: Map<String, Any>): String {
    val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    val channelId = "i.apps.custom.notification"
    val description = "Custom Charging Notification"
    val isFirstUpdate = args["is_first_update"] as Boolean
    val isStillCharging = args["isStillCharging"] as Boolean

    // Create and configure the notification channel
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && isFirstUpdate) {
    val notificationChannel = NotificationChannel(channelId, description, NotificationManager.IMPORTANCE_HIGH)
    notificationChannel.enableLights(true)
    notificationChannel.lightColor = Color.GREEN
    notificationChannel.enableVibration(false)
    notificationManager.createNotificationChannel(notificationChannel)
    }

    // Build the notification
    val builder = NotificationCompat.Builder(this, channelId)
    .setSmallIcon(R.drawable.ic_charging_small)
    .setOngoing(isStillCharging)
    .setPriority(NotificationCompat.PRIORITY_HIGH)
    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
    .setContentTitle("${args["device_name"]}")
    .setContentText("${args["current_percentage"]}% / ${args["estimate_percentage"]}% by ${args["estimate_time"]}")

    // Add a progress bar
    val maxProgress = args["estimate_percentage"].toString().toInt()
    val currentProgress = args["current_percentage"].toString().toInt()


    // Create a custom layout
    val customLayout = RemoteViews(packageName, R.layout.notification_expanded)
    customLayout.setTextViewText(R.id.charging_title, "${args["device_name"]}")
    customLayout.setTextViewText(R.id.charging_estimate, "${args["estimate_percentage"]}% by ${args["estimate_time"]}")
    customLayout.setProgressBar(R.id.charging_progress, maxProgress, currentProgress, false)
    customLayout.setTextViewText(R.id.charging_percentage, "${args["current_percentage"]}%")

    // Set the custom layout
    builder.setCustomBigContentView(customLayout)
    builder.setStyle(NotificationCompat.DecoratedCustomViewStyle())
    // builder.setStyle(MediaStyle())



    // Show the notification


    notificationManager.notify(12345, builder.build())
    return "Success"
    }






    file for layout and UI -> "res/layout/notification_expanded.xml"


    <?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="160dp"
    android:background="@drawable/charging_background_image_dark">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:eek:rientation="vertical"
    android:paddingStart="12dp"
    android:paddingEnd="12dp"
    android:paddingTop="12dp"
    android:paddingBottom="12dp">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:eek:rientation="horizontal">

    <LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:eek:rientation="horizontal">

    <ImageView
    android:layout_width="16dp"
    android:layout_height="16dp"
    android:layout_gravity="center"
    android:src="@drawable/ic_charging"
    android:tint="#FFFFFF" />

    <TextView
    android:id="@+id/charging_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginStart="0dp"
    android:text="Charging"
    android:fontFamily="@font/conigen"
    android:textColor="#FFFFFF"
    android:textSize="16sp"
    android:textStyle="bold" />

    <TextView
    android:id="@+id/charging_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginStart="8dp"
    android:text="Raptrix"

    android:textColor="#08999F"
    android:textSize="20sp"
    android:textStyle="bold|italic" />
    </LinearLayout>

    <TextView
    android:id="@+id/charging_estimate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="80% by 9:30 AM"

    android:layout_gravity="center|right"
    android:textColor="#FFFFFF"
    android:textSize="12sp" />
    </LinearLayout>


    </LinearLayout>

    </LinearLayout>





    I want the solution to customize the font family in the text inside notification.

    Continue reading...

Compartilhe esta Página