Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.1 Using Basic and Multipage Notifications

Hey everyone. This is Paul with Tuts+ and you're watching the Developing for Android Wear course. In this lesson we are going to start on the next set of our series which goes over the different notifications that you can use on a wearable device. So before we begin, let's go ahead and go to wear, source, main res. And we're gonna go down to values and we will add sections for our Wearable List View that will show all the different notifications that we will go over in this course. So we will open up a strings.xml and let's create a set of new string items. So the first will be string name is equal to notification_basic. And we will just call that basic. And then we will also use string name is equal to notification_multipage and that will say Multipage. Next we will use string name is equal to notification_stacked. And that will stay stacked. And then we will have string name notification_reply. And that one will be called voice reply. And then string name is notification_custom_screen and this one will say custom screen. So we can save that and then we can go to stringsarrays.xml and we will change item three. To be at string notification basic, item four will be string notification multi-page, item five can be changed over to be string notification_stacked, item six can be switched over to be string/notification_action. Which it looks like I actually forgot to create action in our strings.xml file. So we'll go back and add that as well. So string name is equal to notification_action. And we'll give that a content of action. Back in our strings array, we'll add in a new item, which will be at string notification reply. And then, item string notification custom screen. So these will be the types of notifications that we will work with in the next set of lessons. So we have six of them here, so to start let's go back to main activity and we'll go down to onClick and we'll add an item for the basic notification. So if you've been working with Android, there's a good chance that you've already worked with notifications, so the first one that we will use is a standard notification like you would use on a phone. And then for the next set, those will all be modifications on the standard notification that works specifically for Android wear. To start let's go with else if, get string, R dot string dot notification basic dot equals ignore case, title. Then we will display a basic notification. So let's use the method name showBasicNotification. And then we will need to create this, so just under onclick, let's go ahead and use private void showBasicNotification. And this will create a notification builder, which we actually wanna use the app compat version of it. So NotificationCompat.builder. We'll call it builder is equal to. And then we're gonna create another method to actually get the builder that we will use. So we will say get base notification builder, and then we will use notification manager compat dot from. We need a past context, so we will use this, and we will say dot notify, we need to pass in an ID, so we will use one just so it is standard across our entire application. And then we have to say builder dot build to create the notification and place that onto the notification manager. So now we need to define getBasedNotificationBuilder. Which we can do just under show basic notification. Here we will use private notification compat dot builder get base notification builder. And this will return new notification compat dot builder, we need to pass in a context so we can use this and then dot set small icon and then let's just go ahead and use r dot mitmap dot iclauncher. And then we'll also say dot set content title as notification title. And then we will use dot set content text as notification text. And we'll throw in our semicolon, and this will create a basic notification when we click on the basic item in our wearable list view. So let's just go ahead and see what this looks like when we run it on our emulator. We'll go ahead and hit OK. We'll scroll down to the basic item, we'll click on that and you'll notice that it does not close our wearable activity. So we will need to do that just so that we can get back to our main screen when these notifications are done. But if we go ahead and close the application, you'll notice that we now have a card here with our notification title, text, and icon. So we can go ahead and close that, and that is the basic notification type that we will be working with. So let's go back to on click, and just after show basic notification, let's add in the line finish so that it will close the application for us. Next, let's go ahead and add in a multiple page notification. So if we come back to on click, we'll say else if get string r dot string dot notification multipage dot equals ignore case to title, we will use show multipage notification. And we will call finish. And then we need to define the show multipage notification method. So let's scroll back down the bottom here, just below get base notification builder, and we will say private void show multipage notification. And what we will want to do here is notification compat dot builder and we'll say builder is equal to get base notification builder, just like we did with the basic notification type. And then we will say, notification, notification page is equal to get base notification builder. And then we're just gonna go ahead and build it. And this will just provide us with a notification that we can use as the pages within our multi-page notification. So let's go ahead and import notification. And then we will use notification manager compat dot from this dot notify one and then builder, which is the notification builder that we created at the beginning of this method dot extend. And what we're gonna do here is we're going to extend a new notification compat wearable extender. Which is what you will use to create wearable specific notifications within your application. And then within that we will use the wearable extender build pattern to say add page, and we'll put in our notification page. And let's go ahead and do this a few times. So we'll just go ahead and paste in add page, so this will give us four pages for our notifications. And then finally, we'll just say dot build. And we'll throw in our last parentheses there. And what this will do is it will show our multi page notification on our wearable device. So let's go ahead and hit Run. We will hit OK and then we will go to our emulator and what we can do is scroll down to the multi-page option and we'll display our multi-page. Notification that will allow us to scroll to the right so that we can see multiple cards within this notification row similar to the rows in the grid view pager. And then if we just swipe back to the left, we can see the other cards that were there and close out this notification. So in this lesson, you've learned about the base notifications that you can display on the wearable device as well as how to add multiple pages to the notifications. So in the next lesson, we will go ahead and look over the stacks notifications types as well as how to add an action button, that you can use to open up a confirmation activity. Or any other actions that you want to use within your application. So I will see you in the next lesson.

Back to the top