Advertisement
Guest User

Notification.kt for dashbuddy

a guest
May 17th, 2025
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.79 KB | None | 0 0
  1. package cloud.trotter.dashbuddy.bubble // You can choose your package structure
  2.  
  3. import android.app.Notification
  4. import android.app.PendingIntent
  5. import android.content.Context
  6. import android.util.Log
  7. import androidx.core.app.NotificationCompat
  8. import androidx.core.app.Person
  9. import androidx.core.content.LocusIdCompat
  10. import androidx.core.graphics.drawable.IconCompat
  11. import java.util.Date
  12.  
  13. /**
  14.  * Helper object to create bubble-specific notifications.
  15.  */
  16. object Notification {
  17.  
  18.     /**
  19.      * Creates a notification configured for display as a bubble.
  20.      *
  21.      * @param context The application context.
  22.      * @param channelId The ID of the notification channel to use.
  23.      * @param senderPerson The Person object representing the sender of the message (e.g., "DashBuddy").
  24.      * @param shortcutId The ID of the shortcut this bubble is associated with.
  25.      * @param bubbleIcon The icon to display for the bubble and notification.
  26.      * @param messageText The text content of the message.
  27.      * @param contentIntent The PendingIntent to launch when the bubble is tapped.
  28.      * @param locusId Optional LocusIdCompat to link the notification to app state.
  29.      * @param desiredHeight The desired height of the bubble's expanded view.
  30.      * @param suppressNotification True to suppress the fly-out notification and only show the bubble.
  31.      * @param autoExpandBubble True to have the bubble auto-expand when it first appears.
  32.      * @return A configured Notification object.
  33.      */
  34.     fun create(
  35.         context: Context,
  36.         channelId: String,
  37.         senderPerson: Person,
  38.         shortcutId: String,
  39.         bubbleIcon: IconCompat,
  40.         messageText: String,
  41.         messagingStyle: NotificationCompat.MessagingStyle,
  42.         contentIntent: PendingIntent,
  43.         locusId: LocusIdCompat? = null, // Added LocusIdCompat parameter
  44.         desiredHeight: Int = 600, // Standard height for bubbles
  45.         suppressNotification: Boolean = true,
  46.         autoExpandBubble: Boolean = true
  47.     ): Notification {
  48.         Log.d("BubbleNotificationHelper", "Creating messaging bubble notification with message: '$messageText', Locus ID: $locusId")
  49.  
  50.         // 1. Create the BubbleMetadata
  51.         val bubbleMetadataBuilder = NotificationCompat.BubbleMetadata.Builder(contentIntent, bubbleIcon)
  52.             .setDesiredHeight(desiredHeight)
  53.             .setSuppressNotification(suppressNotification)
  54.             .setAutoExpandBubble(autoExpandBubble)
  55.  
  56.         // For API 30+, associate LocusId with BubbleMetadata if available
  57.         // Although setLocusId is not directly on BubbleMetadata.Builder,
  58.         // the LocusId on the Notification itself is what matters most for system context.
  59.  
  60.         val bubbleMetadata = bubbleMetadataBuilder.build()
  61.  
  62.         // 2. Create the MessagingStyle for the notification content
  63.         messagingStyle.addMessage(
  64.                 NotificationCompat.MessagingStyle.Message(
  65.                     messageText,
  66.                     Date().time,
  67.                     senderPerson
  68.                 )
  69.             )
  70.  
  71.         // 3. Build the Notification
  72.         val builder = NotificationCompat.Builder(context, channelId)
  73.             .setSmallIcon(bubbleIcon)
  74.             .setContentTitle(senderPerson.name)
  75.             .setContentText(messageText)
  76.             .setShortcutId(shortcutId)
  77.             .setCategory(NotificationCompat.CATEGORY_MESSAGE)
  78.             .addPerson(senderPerson)
  79.             .setStyle(messagingStyle)
  80.             .setBubbleMetadata(bubbleMetadata)
  81.             .setContentIntent(contentIntent)
  82.             .setShowWhen(true)
  83.  
  84.         // Set LocusId on the notification builder if provided (for API 29+)
  85.         if (locusId != null) {
  86.             builder.setLocusId(locusId)
  87.         }
  88.  
  89.         return builder. Build()
  90.     }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement