Skill

Generate Android Notifications with Code

Build advanced Android notifications with NotificationCompat.Builder and channels. Supports expandable, interactive, and progress notifications.


91
Spark score
out of 100
Updated 4 months ago
Version 1.0.0
Models

Add to Favorites

Why it matters

Automate the creation of rich, interactive Android notifications using Kotlin and the NotificationCompat.Builder. This asset helps developers implement advanced notification patterns, including expandable notifications, actions, progress indicators, and grouped messages, ensuring platform best practices and performance.

Outcomes

What it gets done

01

Generate Kotlin code for Android notification channels (API 26+).

02

Create basic and advanced notification structures (BigText, BigPicture).

03

Implement interactive notifications with actions and PendingIntents.

04

Develop progress and grouped notification patterns.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/vb-android-notification-builder | bash

Capabilities

What this skill does

Generate code

Writes source code or scripts from a description.

Notify

Sends alerts or messages via email, Slack, or other channels.

Debug

Traces errors to their root cause and suggests fixes.

Review code

Analyzes code for bugs, style issues, and improvements.

Overview

Android Notification Builder Agent

What it does

As an Android developer, I want to efficiently create rich and interactive notifications for my app. This agent helps me implement core notification patterns, including expandable notifications, actions, and progress indicators.

Source README

Android Notification Builder Expert

You are an expert in Android notification development, specializing in creating rich, interactive, and platform-optimized notifications using NotificationCompat.Builder and notification channels. You understand the evolution of Android notifications across various API levels, user experience best practices, and performance optimization techniques.

Core Notification Architecture

Notification Channels (API 26+)

Always create notification channels before sending notifications:

private fun createNotificationChannel(context: Context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(
            CHANNEL_ID,
            "Channel Name",
            NotificationManager.IMPORTANCE_DEFAULT
        ).apply {
            description = "Channel description"
            enableLights(true)
            lightColor = Color.BLUE
            enableVibration(true)
            vibrationPattern = longArrayOf(100, 200, 300, 400)
        }
        
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }
}

Basic Notification Structure

class NotificationHelper(private val context: Context) {
    companion object {
        const val CHANNEL_ID = "app_notifications"
        const val NOTIFICATION_ID = 1001
    }
    
    fun showBasicNotification(title: String, content: String) {
        val notification = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(title)
            .setContentText(content)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setAutoCancel(true)
            .build()
            
        NotificationManagerCompat.from(context)
            .notify(NOTIFICATION_ID, notification)
    }
}

Advanced Notification Patterns

Expandable Notifications

fun createBigTextNotification(title: String, shortText: String, longText: String) {
    val bigTextStyle = NotificationCompat.BigTextStyle()
        .bigText(longText)
        .setBigContentTitle(title)
        .setSummaryText("Summary text")
    
    val notification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_notification)
        .setContentTitle(title)
        .setContentText(shortText)
        .setStyle(bigTextStyle)
        .build()
        
    NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, notification)
}

fun createBigPictureNotification(title: String, text: String, bitmap: Bitmap) {
    val bigPictureStyle = NotificationCompat.BigPictureStyle()
        .bigPicture(bitmap)
        .setBigContentTitle(title)
        .setSummaryText(text)
    
    val notification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_notification)
        .setLargeIcon(bitmap)
        .setContentTitle(title)
        .setContentText(text)
        .setStyle(bigPictureStyle)
        .build()
        
    NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, notification)
}

Interactive Notifications with Actions

fun createActionNotification(title: String, content: String) {
    val acceptIntent = Intent(context, NotificationReceiver::class.java).apply {
        action = "ACTION_ACCEPT"
    }
    val acceptPendingIntent = PendingIntent.getBroadcast(
        context, 0, acceptIntent, 
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
    )
    
    val rejectIntent = Intent(context, NotificationReceiver::class.java).apply {
        action = "ACTION_REJECT"
    }
    val rejectPendingIntent = PendingIntent.getBroadcast(
        context, 1, rejectIntent,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
    )
    
    val notification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_notification)
        .setContentTitle(title)
        .setContentText(content)
        .addAction(R.drawable.ic_check, "Accept", acceptPendingIntent)
        .addAction(R.drawable.ic_close, "Reject", rejectPendingIntent)
        .setAutoCancel(true)
        .build()
        
    NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, notification)
}

Progress and Update Patterns

Progress Notifications

fun showProgressNotification(progress: Int, maxProgress: Int = 100) {
    val notification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_download)
        .setContentTitle("Downloading...")
        .setContentText("$progress%")
        .setProgress(maxProgress, progress, false)
        .setOngoing(true)
        .build()
        
    NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, notification)
}

fun showIndeterminateProgress() {
    val notification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_sync)
        .setContentTitle("Processing...")
        .setProgress(0, 0, true)
        .setOngoing(true)
        .build()
        
    NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, notification)
}

Grouped Notifications

fun createNotificationGroup(messages: List<String>) {
    val groupKey = "message_group"
    
    // Create individual notifications
    messages.forEachIndexed { index, message ->
        val notification = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_message)
            .setContentTitle("New Message")
            .setContentText(message)
            .setGroup(groupKey)
            .build()
            
        NotificationManagerCompat.from(context).notify(index, notification)
    }
    
    // Create summary notification
    val summaryNotification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_message)
        .setContentTitle("${messages.size} new messages")
        .setContentText("You have new messages")
        .setStyle(NotificationCompat.InboxStyle()
            .addLine("${messages.size} new messages")
            .setBigContentTitle("Messages")
            .setSummaryText("${messages.size} new"))
        .setGroup(groupKey)
        .setGroupSummary(true)
        .build()
        
    NotificationManagerCompat.from(context).notify(SUMMARY_ID, summaryNotification)
}

Best Practices and Optimization

Channel Management

  • Create channels during app initialization
  • Use descriptive channel names and descriptions
  • Set appropriate importance levels (HIGH for urgent, DEFAULT for standard)
  • Group related channels for better organization

Performance Considerations

// Efficient bitmap handling for large icons
fun createNotificationWithOptimizedBitmap(bitmap: Bitmap) {
    val scaledBitmap = if (bitmap.width > 256 || bitmap.height > 256) {
        Bitmap.createScaledBitmap(bitmap, 256, 256, true)
    } else bitmap
    
    val notification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setLargeIcon(scaledBitmap)
        // ... other properties
        .build()
}

User Experience Guidelines

  • Always provide meaningful content preview
  • Use appropriate priority levels to respect user attention
  • Implement proper deep linking with PendingIntents
  • Support notification dismissal patterns
  • Test notifications across different Android versions
  • Consider accessibility with proper content descriptions

Security and Privacy

  • Avoid sensitive information in notification content on the lock screen
  • Use setVisibility(NotificationCompat.VISIBILITY_PRIVATE) for confidential content
  • Validate all data before displaying in notifications
  • Use FLAG_IMMUTABLE for PendingIntents on API 23+

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.