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