xref: /aosp_15_r20/developers/build/prebuilts/gradle/BasicNotifications/README.md (revision d353a188ca6ec4b5eba25b5fbd7bcb8ce61322fb)
1*d353a188SXin Li
2*d353a188SXin LiAndroid BasicNotifications Sample
3*d353a188SXin Li===================================
4*d353a188SXin Li
5*d353a188SXin LiA basic app showing how to display events in the system's notification bar using
6*d353a188SXin Lithe NotificationCompat API.
7*d353a188SXin LiNotificationCompat API is used for compatibility with older devices, running Android
8*d353a188SXin Li1.6 (Donut) (API level 4) or newer.
9*d353a188SXin Li
10*d353a188SXin LiIntroduction
11*d353a188SXin Li------------
12*d353a188SXin Li
13*d353a188SXin LiThe [Notification API][1] allows the app developers to display a message outside
14*d353a188SXin Liof your application's normal UI.
15*d353a188SXin Li
16*d353a188SXin LiThe class [Notification][2] was added in the Android 3.0 (API level 11), but this
17*d353a188SXin Lisample refers to the [NotificationCompat][3] class (part of the [support library][4]),
18*d353a188SXin Li which offers the same functionality for Android 1.6 (API level 4) or newer.
19*d353a188SXin Li
20*d353a188SXin LiA Notificaiton can be created using Notification.Builder object.
21*d353a188SXin LiAt bare minimum, a Builder object must include the following:
22*d353a188SXin Li- A small icon, set by [setSmallIcon()][5]
23*d353a188SXin Li- A title, set by [setContentTitle()][6]
24*d353a188SXin Li- Detail text, set by [setContentText()][7]
25*d353a188SXin Li
26*d353a188SXin Liin the code snippet, it looks like following.
27*d353a188SXin Li```java
28*d353a188SXin LiNotificationCompat.Builder builder = new NotificationCompat.Builder(this);
29*d353a188SXin Libuilder.setSmallIcon(R.drawable.ic_stat_notification);
30*d353a188SXin Libuilder.setContentTitle("BasicNotifications Sample");
31*d353a188SXin Libuilder.setContentText("Time to learn about notifications!");
32*d353a188SXin Li```
33*d353a188SXin Li
34*d353a188SXin LiTo issue the notification, call notify() method in the [NotificationManager][8].
35*d353a188SXin LiThe code snippet will immediately display the notification icon in the
36*d353a188SXin Linotification bar.
37*d353a188SXin Li
38*d353a188SXin Li```java
39*d353a188SXin LiNotificationManager notificationManager = (NotificationManager) getSystemService(
40*d353a188SXin Li        NOTIFICATION_SERVICE);
41*d353a188SXin LinotificationManager.notify(NOTIFICATION_ID, builder.build());
42*d353a188SXin Li```
43*d353a188SXin Li
44*d353a188SXin Li[1]: http://developer.android.com/guide/topics/ui/notifiers/notifications.html
45*d353a188SXin Li[2]: http://developer.android.com/reference/android/app/Notification.html
46*d353a188SXin Li[3]: http://developer.android.com/reference/android/support/v4/app/NotificationCompat.html
47*d353a188SXin Li[4]: http://developer.android.com/tools/support-library/index.html
48*d353a188SXin Li[5]: http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setSmallIcon(int)
49*d353a188SXin Li[6]: http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setContentTitle(java.lang.CharSequence)
50*d353a188SXin Li[7]: http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setContentText(java.lang.CharSequence)
51*d353a188SXin Li[8]: http://developer.android.com/reference/android/app/NotificationManager.html
52*d353a188SXin Li
53*d353a188SXin LiPre-requisites
54*d353a188SXin Li--------------
55*d353a188SXin Li
56*d353a188SXin Li- Android SDK 27
57*d353a188SXin Li- Android Build Tools v27.0.2
58*d353a188SXin Li- Android Support Repository
59*d353a188SXin Li
60*d353a188SXin LiScreenshots
61*d353a188SXin Li-------------
62*d353a188SXin Li
63*d353a188SXin Li<img src="screenshots/main.png" height="400" alt="Screenshot"/>
64*d353a188SXin Li
65*d353a188SXin LiGetting Started
66*d353a188SXin Li---------------
67*d353a188SXin Li
68*d353a188SXin LiThis sample uses the Gradle build system. To build this project, use the
69*d353a188SXin Li"gradlew build" command or use "Import Project" in Android Studio.
70*d353a188SXin Li
71*d353a188SXin LiSupport
72*d353a188SXin Li-------
73*d353a188SXin Li
74*d353a188SXin Li- Google+ Community: https://plus.google.com/communities/105153134372062985968
75*d353a188SXin Li- Stack Overflow: http://stackoverflow.com/questions/tagged/android
76*d353a188SXin Li
77*d353a188SXin LiIf you've found an error in this sample, please file an issue:
78*d353a188SXin Lihttps://github.com/googlesamples/android-BasicNotifications
79*d353a188SXin Li
80*d353a188SXin LiPatches are encouraged, and may be submitted by forking this project and
81*d353a188SXin Lisubmitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
82*d353a188SXin Li
83*d353a188SXin LiLicense
84*d353a188SXin Li-------
85*d353a188SXin Li
86*d353a188SXin LiCopyright 2017 The Android Open Source Project, Inc.
87*d353a188SXin Li
88*d353a188SXin LiLicensed to the Apache Software Foundation (ASF) under one or more contributor
89*d353a188SXin Lilicense agreements.  See the NOTICE file distributed with this work for
90*d353a188SXin Liadditional information regarding copyright ownership.  The ASF licenses this
91*d353a188SXin Lifile to you under the Apache License, Version 2.0 (the "License"); you may not
92*d353a188SXin Liuse this file except in compliance with the License.  You may obtain a copy of
93*d353a188SXin Lithe License at
94*d353a188SXin Li
95*d353a188SXin Lihttp://www.apache.org/licenses/LICENSE-2.0
96*d353a188SXin Li
97*d353a188SXin LiUnless required by applicable law or agreed to in writing, software
98*d353a188SXin Lidistributed under the License is distributed on an "AS IS" BASIS, WITHOUT
99*d353a188SXin LiWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
100*d353a188SXin LiLicense for the specific language governing permissions and limitations under
101*d353a188SXin Lithe License.
102