Join Joe Marini for an in-depth discussion in this video Advanced message options, part of Learning Firebase Cloud Messaging for Android.
- [Instructor] Now that we've seen how to send our first message, let's go back to the console and take a look at some of the more advanced options. So here in the notifications composer, I'm going to create a new message and I'll fill up the same fields as last time, so I'll give it a message, this is another message. And I'll call this FCM Message 4 and I'll leave that as Send Now. I'll paste in my device token. But now let's take a look at some of the advanced options.
So I'll expand this section here. When it expands, you can see that I have a few more fields that I can fill out. I can enter a title which will be shown to the user as the notification title. Remember in the last example, it just used the name of the app so I'll just simply say Hello FCM Example. There is also a place for me to enter some custom data, so some key value pairs. I can also set the message priority and the expiration date, and even the sound that I want it to make.
I'll go ahead and give the message a title which I've already done, and I'm going to give it some key value pairs, so I'll just type in foo and bar. I'll just leave everything else the same. Before we continue, just a quick note on how this data gets delivered. FCM will receive the message and create the notification. And it will associate this data with the intent that gets fired off to your app when the notification is tapped by the user.
Your app can extract that data and use it however you wish. So these key value pairs will be passed into your app. Let's just quickly switch over to Android studio and I'm going to add some code to extract this data and display it in the launch activity. And just quickly if we go to the layout, remember that in my layout here, I have a little text field right here that displays the launch information.
In my MainActivity, I'm going to scroll down to this section right here. When the activity starts up, I'm going to get the launch intent and get any launch intent extras. If that value is not null, then some data was passed to my app. Let's go ahead and build up a string that will contain each of the keys that were passed in by the intent. I'll just write String launchMsg.
And we'll make that an empty string to start. Then I'll have a for loop so I'll get get the keySet for each one of these messages and then in my loop, I'll create an object just to hold the value. From the intent, I'm going to get the key that I just retrieved and I'm going to go ahead and log that out.
I'm going to log out the key and the value, so key plus the key and then the value with whatever the value was with a carriage return. Then in my launchMsg, I'm going to add whatever this was as well. There we go. Then after that, I'll just simply say tvMsg .setText to be whatever my launchMsg was.
And tvMsg is retrieved up here in my onCreate. So I have a TextView and this is my TextView right here that's in my activity. All right, so that's all the rest of that. Let's save. And now I'm going to run the app. I'll bring up the Logcat and let's go over to the emulator. Let's make sure that the app is in the background because remember it has to be in the background to retrieve the message from the console.
Let's jump back over to the console. All right, let's go ahead and click on send the message. Everything looks fine, so I'll click on send. So let's go to the emulator now. You can see there's the notification and if I pull the notification shade down, you can see now that there's my title, there's the Hello FCM Example and there's the text for another message. If I tap on it, you can see that here's the data that was passed into my app when it was started up.
If you look closely, there's the foo and there's the bar right in there. That's the custom data key value pair that I sent from the notification composer. This is a really great feature of FCM because it means that you can send specialized messages to your app and have it take different actions based upon the message and you can give this capability to marketing people in your team who don't need programming skills in order to make it work.