- Overview
- Transcript
3.2 Stacking Multiple Notifications and Using an Action Button
In this lesson you will learn how to group multiple notifications together in order to prevent spamming your user. You will also learn about the ConfirmationActivity
and how to add an action button to a wearable notification.
1.Introduction and Getting Set Up3 lessons, 11:45
1.1Introduction02:46
1.2Setting Up Emulators and Hello World04:40
1.3Debugging on a Hardware Device04:19
2.Wearable App UI Components6 lessons, 1:15:54
2.1Round and Square Device Support06:37
2.2WearableListViews16:58
2.3DelayedConfirmationView10:06
2.4GridViewPager: Part 116:32
2.5GridViewPager: Part 216:10
2.6WearableActivity and Ambient Mode09:31
3.Wearable Notifications4 lessons, 38:35
3.1Using Basic and Multipage Notifications09:33
3.2Stacking Multiple Notifications and Using an Action Button10:04
3.3Replying With Speech, Emojis and Predefined Options08:57
3.4Creating Custom UI Notifications10:01
4.Creating a Watch Face2 lessons, 34:28
4.1Starting a Digital Watch Face16:57
4.2Handling Watch Face States17:31
5.Conclusion1 lesson, 02:14
5.1Conclusion02:14
3.2 Stacking Multiple Notifications and Using an Action Button
Hey everyone. This is Paul with Tuts+ and you're watching the developing for Android wear course. In this lesson, we will look at two more notification types. The first will be the stack notification type, which is generally used when you have messaging apps. So that you can send multiple notifications to a wearable device and they will be stacked on to one display card. And the other type will be the action button notification, which is a way of adding a button that the user can scroll to, and they can click on that to perform an action based off whatever your application needs it to do. So to start, let's go back to on click, and where we have our if statements for the different items clicked in our wearable list view. Let's go ahead and add a new one. So, else if getString R.string.notification_stacked.equals ignore case title. We will use show, stacked, notification, and we'll call finish. So that the activity will close for us when we display our stacked notification, and then let's go ahead and do find, show stacked notification. So let's scroll down a little bit here, and we'll use private void show stats notification so the first thing we'll need to do is create a notification builder. So we'll go ahead and use notificationcoompat.builder, builder is equal to get base notification builder, and then we need to set a group for the notification builder. So that any other notifications that are built using the same name key that we name our group with will all be stacked into one notification card. So we will use builder dot set group and we will just use the word key as our key for now. Now, you can use whatever you want, as long as it works with your notifications. So let's say you had an e-mail subject and you are updating it with multiple replies. You can use that subject as the key. So now we will create a notification. So notification, we'll call it notification is equal to builder.build So now we will launch this notification using the notification manager compad. So we'll go ahead and type in notification manager compad.from, we'll pass on this as the context and then notify with our ID of one, and the notification that we just created. Now that the base notification is created, we can go ahead and create another builder. So builder is equal to get base notification builder. We'll use the set group again, with the same key, and then we will create another notification using builder dot build. Since this has a group set to it, we can go ahead and use notificationManagerCompat.this.notify, and we'll pass in the number two so that it doesn't override our last notification, and we'll pass in this new notification. And then we'll do the same exact thing again, but this time the notification id will be three. So at the end of this, we'll actually have three notifications that are all stacked on top of each other on the Wearable notification stream. Now, if we want to have the same effect on the phone as well as the Wearable device we can go ahead and use builder = gitbasednotificationbuilder, and we'll use .setGroup. We'll pass in the same key, .setgroupsummary as true.setstyle as a new notification compact.inboxstyle, and then we can set big content title as the title. .setsummarytext as the text for the stack notifications, and then we can add a set of lines to represent each of the messages that have come in. So we can say .addlinemessage1.dotaddline for message two and .addline for message three. And then finally, we'll use a semicolon and we'll launch this notification by using notification manager compat.from, pass in our context, dot notify. We'll pass in the id of four, and the notification, which will be builder.build(). So one important thing to notice about all of these notifications that we're sending off right now is that they could be sent from either the phone or the watch. So if your watch doesn't have any native components to it like we've built earlier in this course. Then you can actually launch these notifications from your standard Android application, and they'll show up on the watch, even with these new styles that we're using, and that's it for the stack notification. So, if we come back to on click, we'll go ahead and add another line, here. Else, if, get string, r.string.notification action dot equals IgnoreCase for title, then we'll go ahead and use showActionnotification, and then we'll call finish. So let's go ahead and define showActionnotification now. So we'll scroll back down to the bottom here And private void showActionNotification, and we'll go ahead and create our base builder. So we'll say NotificationCompat.Builder, builder is equal to getBasedNotificationBuilder, and the way the actions work is you actually create a pending intent based off of another intent for the action button once it is pressed. So when the button is pressed, it will shoot off an intent to handle whatever you're trying to do with it. So for this notification we'll actually just go ahead and start up another activity. Though in the next lesson, we will use the action button so that we can send voice replies over to our application. So let's go ahead and create our intent. So intent, intent is equal to new intent. We'll pass in our context and we'll use the confirmation activity.class which is an activity that was created by Google for wearable applications to display a check mark whenever an action is finished. So we'll go ahead and put in our semicolon and then we will use intent.putextra. Confirmation activity.extra animation type which is a special kind of key that they use so you can determine what kind of animation will be used. So we're gonna use confirmation activity.success animation and then we'll also use intent.putextra confirmation activity .EXTRA_MESSAGE, and we're gonna just say, success, with an exclamation point. And then we will wrap this intent with a pending intent by saying, PendingIntent pendingIntent = PendingIntent.getActivities( this, 0, intent, PendingIntent. Flag, update current. Now we will take our notification builder and use builder.addaction, new notification compat.action and we'll pass it an icon. So R.mitmap.icylauncher and we'll give it a title and we'll just say it's the action title, and then we need to pass in our pending intent, and then we can launch this notification. So we'll just use notification manager compact dot from this dot notify with the id of one, and then we'll build the notification using the builder. Now before we can run this application, we actually need to let the app know that we will use confirmation activity by adding it into our manifest. So let's go back to where/source/main/in Android manifest dot XML, and then under our application node, with the other activities, let's add one more. So we'll say activity and the name will be Android.support.wearable.activity.confirm- ationactivity. And we will just close this off we will save it looks like we have a red line in main activity so let's find out what we missed. It looks like I defined our pending intent wrong. So, let's go ahead and see what's going on with that. It looks like I used get activities, instead of get activity. So let's just go ahead and fix that, and everything looks better now. So we'll go ahead and click on run, and then we'll hit OK, and go to our emulator and we can scroll down to the stacked option, so we'll click on that. And as you can see our notification actually has a plus two more here, so you can see the different notifications that were created for our stack notification. And if we go back to our application, we can scroll down, and we'll go to the action type. And then we can see that we have a new notification that is not a stacked type, but we can scroll over to the right and there's a button with the action title that we used. And if we click on that it creates the confirmation activity that says success and just animates a check mark for us. So in this lesson you've learned about the action notification and the stack notification. And in the next lesson, we will look over how to add voice replies as well as quick replies to a notification. So I will see you in the next lesson.