README.md
1
2Android ElizaChat Sample
3===================================
4
5A basic sample showing how to add extensions to notifications on wearable using
6[NotificationCompat.WearableExtender][1] API by providing a chat experience.
7
8Introduction
9------------
10
11[NotificationCompat.WearableExtender][1] API offers you a functionality to
12 add wearable extensions to notifications like [Add the Voice Input as a Notification Action][2].
13
14This example also adds the wearable notifications as a voice input by the following code:
15
16```java
17NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
18 .setContentTitle(getString(R.string.eliza))
19 .setContentText(mLastResponse)
20 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_eliza))
21 .setSmallIcon(R.drawable.bg_eliza)
22 .setPriority(NotificationCompat.PRIORITY_MIN);
23
24Intent intent = new Intent(ACTION_RESPONSE);
25PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent,
26 PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT);
27Notification notification = builder
28 .extend(new NotificationCompat.WearableExtender()
29 .addAction(new NotificationCompat.Action.Builder(
30 R.drawable.ic_full_reply, getString(R.string.reply), pendingIntent)
31 .addRemoteInput(new RemoteInput.Builder(EXTRA_REPLY)
32 .setLabel(getString(R.string.reply))
33 .build())
34 .build()))
35 .build();
36NotificationManagerCompat.from(this).notify(0, notification);
37```
38
39When you issue this notification, users can swipe to the left to see the "Reply" action button.
40
41When you receive the response, you can do it by the following code:
42
43```java
44@Override
45public int onStartCommand(Intent intent, int flags, int startId) {
46 if (null == intent || null == intent.getAction()) {
47 return Service.START_STICKY;
48 }
49 String action = intent.getAction();
50 if (action.equals(ACTION_RESPONSE)) {
51 Bundle remoteInputResults = RemoteInput.getResultsFromIntent(intent);
52 CharSequence replyMessage = "";
53 if (remoteInputResults != null) {
54 replyMessage = remoteInputResults.getCharSequence(EXTRA_REPLY);
55 }
56 processIncoming(replyMessage.toString());
57 } else if (action.equals(MainActivity.ACTION_GET_CONVERSATION)) {
58 broadcastMessage(mCompleteConversation.toString());
59 }
60 return Service.START_STICKY;
61}
62```
63
64[1]: https://developer.android.com/reference/android/support/v4/app/NotificationCompat.WearableExtender.html
65[2]: https://developer.android.com/training/wearables/notifications/voice-input.html#AddAction
66
67Pre-requisites
68--------------
69
70- Android SDK 27
71- Android Build Tools v27.0.2
72- Android Support Repository
73
74Screenshots
75-------------
76
77<img src="screenshots/companion_eliza_chat_response.png" height="400" alt="Screenshot"/> <img src="screenshots/companion_eliza_chat.png" height="400" alt="Screenshot"/> <img src="screenshots/wearable_eliza_notification.png" height="400" alt="Screenshot"/> <img src="screenshots/wearable_voice_reply.png" height="400" alt="Screenshot"/>
78
79Getting Started
80---------------
81
82This sample uses the Gradle build system. To build this project, use the
83"gradlew build" command or use "Import Project" in Android Studio.
84
85Support
86-------
87
88- Google+ Community: https://plus.google.com/communities/105153134372062985968
89- Stack Overflow: http://stackoverflow.com/questions/tagged/android
90
91If you've found an error in this sample, please file an issue:
92https://github.com/googlesamples/android-ElizaChat
93
94Patches are encouraged, and may be submitted by forking this project and
95submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
96
97License
98-------
99
100Copyright 2017 The Android Open Source Project, Inc.
101
102Licensed to the Apache Software Foundation (ASF) under one or more contributor
103license agreements. See the NOTICE file distributed with this work for
104additional information regarding copyright ownership. The ASF licenses this
105file to you under the Apache License, Version 2.0 (the "License"); you may not
106use this file except in compliance with the License. You may obtain a copy of
107the License at
108
109http://www.apache.org/licenses/LICENSE-2.0
110
111Unless required by applicable law or agreed to in writing, software
112distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
113WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
114License for the specific language governing permissions and limitations under
115the License.
116