xref: /aosp_15_r20/developers/build/prebuilts/gradle/SynchronizedNotifications/README.md (revision d353a188ca6ec4b5eba25b5fbd7bcb8ce61322fb)
1*d353a188SXin Li
2*d353a188SXin LiAndroid SynchronizedNotifications Sample
3*d353a188SXin Li===================================
4*d353a188SXin Li
5*d353a188SXin LiA basic sample showing how to use simple or synchronized notifications.
6*d353a188SXin LiThis allows users to dismiss events from either their phone or wearable device simultaneously.
7*d353a188SXin Li
8*d353a188SXin LiIntroduction
9*d353a188SXin Li------------
10*d353a188SXin Li
11*d353a188SXin LiThe [DataAPI][1] exposes an API for components to read or write data items and assets between
12*d353a188SXin Lithe handhelds and wearables. A [DataItem][2] is synchronized across all devices in an Android Wear network.
13*d353a188SXin LiIt is possible to set data items while not connected to any nodes. Those data items will be synchronized
14*d353a188SXin Liwhen the nodes eventually come online.
15*d353a188SXin Li
16*d353a188SXin LiThis example presents three buttons that would trigger three different combinations of
17*d353a188SXin Linotifications on the handset and the watch:
18*d353a188SXin Li
19*d353a188SXin Li1. The first button builds a simple local-only notification on the handset.
20*d353a188SXin Li2. The second one creates a wearable-only notification by putting a data item in the shared data
21*d353a188SXin Listore and having a [com.google.android.gms.wearable.WearableListenerService][3] listen for
22*d353a188SXin Lithat on the wearable.
23*d353a188SXin Li3. The third one creates a local notification and a wearable notification by combining the above
24*d353a188SXin Litwo. It, however, demonstrates how one can set things up so that the dismissal of one
25*d353a188SXin Linotification results in the dismissal of the other one.
26*d353a188SXin Li
27*d353a188SXin LiIn the #2 and #3 items, the following code is used to synchronize the data between the handheld
28*d353a188SXin Liand the wearable devices using DataAPI.
29*d353a188SXin Li
30*d353a188SXin Li```java
31*d353a188SXin LiPutDataMapRequest putDataMapRequest = PutDataMapRequest.create(path);
32*d353a188SXin LiputDataMapRequest.getDataMap().putString(Constants.KEY_CONTENT, content);
33*d353a188SXin LiputDataMapRequest.getDataMap().putString(Constants.KEY_TITLE, title);
34*d353a188SXin LiPutDataRequest request = putDataMapRequest.asPutDataRequest();
35*d353a188SXin LiWearable.DataApi.putDataItem(mGoogleApiClient, request)
36*d353a188SXin Li        .setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
37*d353a188SXin Li            @Override
38*d353a188SXin Li            public void onResult(DataApi.DataItemResult dataItemResult) {
39*d353a188SXin Li                if (!dataItemResult.getStatus().isSuccess()) {
40*d353a188SXin Li                    Log.e(TAG, "buildWatchOnlyNotification(): Failed to set the data, "
41*d353a188SXin Li                            + "status: " + dataItemResult.getStatus().getStatusCode());
42*d353a188SXin Li                }
43*d353a188SXin Li            }
44*d353a188SXin Li        });
45*d353a188SXin Li```
46*d353a188SXin Li
47*d353a188SXin Li[1]: http://developer.android.com/reference/com/google/android/gms/wearable/DataApi.html#putDataItem(com.google.android.gms.common.api.GoogleApiClient%2C%20com.google.android.gms.wearable.PutDataRequest)
48*d353a188SXin Li[2]: http://developer.android.com/reference/com/google/android/gms/wearable/DataItem.html
49*d353a188SXin Li[3]: https://developer.android.com/reference/com/google/android/gms/wearable/WearableListenerService.html
50*d353a188SXin Li
51*d353a188SXin LiPre-requisites
52*d353a188SXin Li--------------
53*d353a188SXin Li
54*d353a188SXin Li- Android SDK 27
55*d353a188SXin Li- Android Build Tools v27.0.2
56*d353a188SXin Li- Android Support Repository
57*d353a188SXin Li
58*d353a188SXin LiScreenshots
59*d353a188SXin Li-------------
60*d353a188SXin Li
61*d353a188SXin Li<img src="screenshots/different_notifications_phone.png" height="400" alt="Screenshot"/> <img src="screenshots/different_notifications_wearable.png" height="400" alt="Screenshot"/> <img src="screenshots/notification_options.png" height="400" alt="Screenshot"/> <img src="screenshots/watch_only_notification.png" height="400" alt="Screenshot"/>
62*d353a188SXin Li
63*d353a188SXin LiGetting Started
64*d353a188SXin Li---------------
65*d353a188SXin Li
66*d353a188SXin LiThis sample uses the Gradle build system. To build this project, use the
67*d353a188SXin Li"gradlew build" command or use "Import Project" in Android Studio.
68*d353a188SXin Li
69*d353a188SXin LiSupport
70*d353a188SXin Li-------
71*d353a188SXin Li
72*d353a188SXin Li- Google+ Community: https://plus.google.com/communities/105153134372062985968
73*d353a188SXin Li- Stack Overflow: http://stackoverflow.com/questions/tagged/android
74*d353a188SXin Li
75*d353a188SXin LiIf you've found an error in this sample, please file an issue:
76*d353a188SXin Lihttps://github.com/googlesamples/android-SynchronizedNotifications
77*d353a188SXin Li
78*d353a188SXin LiPatches are encouraged, and may be submitted by forking this project and
79*d353a188SXin Lisubmitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
80*d353a188SXin Li
81*d353a188SXin LiLicense
82*d353a188SXin Li-------
83*d353a188SXin Li
84*d353a188SXin LiCopyright 2017 The Android Open Source Project, Inc.
85*d353a188SXin Li
86*d353a188SXin LiLicensed to the Apache Software Foundation (ASF) under one or more contributor
87*d353a188SXin Lilicense agreements.  See the NOTICE file distributed with this work for
88*d353a188SXin Liadditional information regarding copyright ownership.  The ASF licenses this
89*d353a188SXin Lifile to you under the Apache License, Version 2.0 (the "License"); you may not
90*d353a188SXin Liuse this file except in compliance with the License.  You may obtain a copy of
91*d353a188SXin Lithe License at
92*d353a188SXin Li
93*d353a188SXin Lihttp://www.apache.org/licenses/LICENSE-2.0
94*d353a188SXin Li
95*d353a188SXin LiUnless required by applicable law or agreed to in writing, software
96*d353a188SXin Lidistributed under the License is distributed on an "AS IS" BASIS, WITHOUT
97*d353a188SXin LiWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
98*d353a188SXin LiLicense for the specific language governing permissions and limitations under
99*d353a188SXin Lithe License.
100