Additional notification code snippets migration#929
Conversation
Change-Id: Ib7b51173d55bf49626fe9b19460e4d04d6994470
There was a problem hiding this comment.
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.
| val inputStream = context.contentResolver.openInputStream(imageUri) | ||
| val bitmap = BitmapFactory.decodeStream(inputStream) | ||
| // Display the image | ||
| // ... |
There was a problem hiding this comment.
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
// ...
}| val notificationManager: NotificationManager = | ||
| context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
| notificationManager.createNotificationChannel(channel) |
There was a problem hiding this comment.
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.
| 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" |
There was a problem hiding this comment.
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.
| val KEY_TEXT_REPLY = "key_text_reply" | |
| val KEY_TEXT_REPLY = ReplyReceiver.KEY_TEXT_REPLY |
Change-Id: Ia2c1c24f4c11b8273210a641485c90fb17985c54
Change-Id: Ib7b51173d55bf49626fe9b19460e4d04d6994470
For documentation in: https://developer.android.com/develop/ui/compose/notifications/create-notification