Pro Android with Kotlin
Download 5.42 Mb. Pdf ko'rish
|
@de android telegram Pro Android with Kotlin Developing Modern Mobile
144
CHAPTER 8: APIs Figure 8-1. Smartphone notification Figure 8-2. Notification content 145 CHAPTER 8: APIs Notifications also allow for actions, such as calling custom activities when tapped, or they can contain special action buttons or even edit fields a user can fill out. Likewise, although notifications were primarily built to show only short text snippets, with current Android versions, it is possible to present longer text there as well. The online API documentation suggests to use the NotificationCompat API from the support library. Using this compatibility layer allows older versions to present similar or no- op variants on features that were made available only later, simplifying the development. Although using this compatibility layer removes the burden from the developer of presenting many branches inside the code to take care of different Android API levels, caution must be taken to not make an app unusable by depending too much on the latest notification API features. To make sure the compatibility API is available for your project inside Android Studio, check the build.gradle setting of your module in the dependencies section (only one line; remove the newline after implementation). implementation "com.android.support:support-compat:27.0.2" The following sections present an outline of the notification API. With this API having grown considerably during the last years, please consult the online documentation for a more detailed description of all notification features. Creating and Showing Notifications To create and show a notification, you prepare action intents for a tap and additional action buttons, use a notification builder to construct the notification, register a notification channel, and finally make the framework show the notification. An example looks like this: val NOTIFICATION_CHANNEL_ID = "1" val NOTIFICATION_ID = 1 // Make sure this Activity exists val intent = Intent(this, AlertDetails::class.java) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK //or Intent.FLAG_ACTIVITY_CLEAR_TASK val tapIntent = PendingIntent.getActivity(this, 0, intent, 0) // Make sure this broadcast receiver exists and can // be called by explicit Intent like this val actionIntent = Intent(this, MyReceiver::class.java) actionIntent.action = "com.xyz.MAIN" actionIntent.putExtra(EXTRA_NOTIFICATION_ID, 0) val actionPendingIntent = PendingIntent.getBroadcast(this, 0, actionIntent, 0) val builder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID) .setSmallIcon( ... an icon resource id... ) |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling