Skip to content

Additional notification code snippets migration#929

Open
alabiaga wants to merge 2 commits into
android:mainfrom
alabiaga:notification_snippets
Open

Additional notification code snippets migration#929
alabiaga wants to merge 2 commits into
android:mainfrom
alabiaga:notification_snippets

Conversation

@alabiaga
Copy link
Copy Markdown
Member

Change-Id: Ib7b51173d55bf49626fe9b19460e4d04d6994470

For documentation in: https://developer.android.com/develop/ui/compose/notifications/create-notification

Change-Id: Ib7b51173d55bf49626fe9b19460e4d04d6994470
@alabiaga alabiaga requested a review from a team as a code owner May 22, 2026 19:03
@alabiaga alabiaga requested a review from raystatic May 22, 2026 19:03
@snippet-bot
Copy link
Copy Markdown

snippet-bot Bot commented May 22, 2026

Here is the summary of changes.

You are about to add 15 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces comprehensive notification snippets, including channel creation, basic and styled notifications, tap actions, and reply handling with a dedicated ReplyReceiver. Feedback focuses on addressing a potential resource leak by properly closing an InputStream in ReplyReceiver, improving type safety when accessing the NotificationManager, and using shared constants for notification reply keys to ensure consistency.

Comment on lines +36 to +39
val inputStream = context.contentResolver.openInputStream(imageUri)
val bitmap = BitmapFactory.decodeStream(inputStream)
// Display the image
// ...
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The InputStream obtained from openInputStream is not closed, which can lead to resource leaks. Use the .use extension function to ensure the stream is closed automatically even if an exception occurs during processing.

                context.contentResolver.openInputStream(imageUri)?.use { inputStream ->
                    val bitmap = BitmapFactory.decodeStream(inputStream)
                    // Display the image
                    // ...
                }

Comment on lines +111 to +113
val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better type safety and consistency with modern Android practices, use context.getSystemService(NotificationManager::class.java). This avoids manual casting and is safer when the SDK version is 23 or higher.

Suggested change
val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
val notificationManager = context.getSystemService(NotificationManager::class.java)
notificationManager?.createNotificationChannel(channel)


// [START android_notification_add_reply]
// Key for the string that's delivered in the action's intent.
val KEY_TEXT_REPLY = "key_text_reply"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It is recommended to use the constant ReplyReceiver.KEY_TEXT_REPLY instead of a hardcoded string. This ensures that the key used to send the reply matches the key used to retrieve it in the receiver.

Suggested change
val KEY_TEXT_REPLY = "key_text_reply"
val KEY_TEXT_REPLY = ReplyReceiver.KEY_TEXT_REPLY

Change-Id: Ia2c1c24f4c11b8273210a641485c90fb17985c54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant