Codementor Events

Sending Push Notifications with Firebase in Android (Part 2)

Published Mar 07, 2017Last updated Mar 08, 2017
Sending Push Notifications with Firebase in Android (Part 2)

If you have read the tutorial Send Push Notifications with Firebase in Android (Part 1), you can go on with this part and learn more details.

In the last tutorial, I showed how you can prepare your project for integrating Firebase Cloud Messaging (FCM) and how to send a push notification from the Firebase console. Today, I want to show you more possibilities in the message sending dialog in the Firebase console.

Timing

With Firebase Console, you have the option to send your push notification all at once. But sometimes, you might want to send push notification at a very specific time. Firebase has a convenient out-of-the-box function just for that.

For example, if you want to inform your app users about something at lunch time, you can choose "Send Later" for the "Delivery date" and set the time. The default checkbox "Recipient time zone" can guarantee that all the receivers will receive it during lunch at their local time. This a very good feature because you wouldn't disturb any users at night with a push notification. Of course, you can also select the time for a special recipient time zone. This way, the message will be delivered to your users at a specific time according to the time zone selection.

enter image description here

Targeting

Normally, you wouldn't send push notifications to all your users. Firebase Cloud Messaging (FCM) offers three different types of targets, which allows you to send push notifications to a specified user group. The three types of targets are: user segment, topic, and single device. We will cover all three:

User Segment

Here, you can choose an app for sending out push notifications. Click "AND" to add more filter rules.

  • Audience - you can send push notifications to all the users or only to those who've made purchases.
    Push Notification Firebase in Android Audience

  • Version - you can also set filters for the version of your apps that will receive this push notification

enter image description here

  • Language - you can filter the receiver group by the language setting on their devices. For example, if you only want to the Swedish-speaking people to receive a certain notification, select "Swedish" in the dropdown menu. Keep in mind, your app must support this language!

Topic

If you have a sport related app, perhaps some of your users would want to receive baseball-related push notifications, while others only want to receive notifications related to football. To adapt to different interests of different users groups, you should set up different categories for these push notifications.
"Topic" is a very useful feature for app developers and users. You can think of it like a kind of subscription channel that directs push notification to corresponding user groups.

(1) Subscribe to a Topic

If you want your users to receive notifications by topic, you can add the following line in your apps code so the user can select his or her interest(s):

FirebaseMessaging.getInstance().subscribeToTopic("TopicName");   //use a topic name that suits this user

Here, you would need to change the "TopicName" to a topic that your users might be interested in, e.g. "football".

(2) Send Push Notifications by Topic

The topic name normally shows up one day after the topic has been registered, i.e. the subscribeToTopic ("TopicName"). If you see this message when you're trying to select a topic in your Firebase console: "This project does not have any topics," don't panic! Wait for a day and it will magically appear in the dropdown list, "Topic."

enter image description here

Single Device

As described in the FirebaseInstanceId document,
Firebase Instance ID provides a unique identifier for each app instance and a mechanism to authenticate and authorize actions (example: sending FCM messages).

Instance ID is stable except when:

  • App deletes Instance ID
  • App is restored on a new device
  • User uninstalls/reinstall the app
  • User clears app data

You can think of it like Apple's device ID.

To find your instance ID, pay attention to the Android monitor when you start your app for the first time after you've added the service that extends FirebaseInstanceIdService (read more about it here. This service must exist in your project for you to get an instance ID.

If you want to see the ID every time you open your app, you can add the following line in the onCreate() function in your app and read it from the log.

 Log.d("FCM", "Instance ID: " + FirebaseInstanceId.getInstance().getToken());

For the example from Send Push Notifications with Firebase in Android (Part 1), you can also send the instance ID to your own server through function sendRegistrationToServer in the service that extends FirebaseInstanceIdService. Since it can be connected with other user information, you can add more possibilities to enable users to send notification to each other. I haven't tried that myself but maybe you'll have a better idea on how you can benefit from this function!

Ok. When you have your instance ID, you can test it by sending a message to a device of your choice.

enter image description here

Advanced Options

If you're looking for more ways to customize your push notifications, check out "Advanced Options".

enter image description here

  • Title - You can set the title for your push notifications. This is available to Android devices and Apple Watch.
  • Custom data - You can send key-value pairs data along with your push notification. In your service, which extends FirebaseMessagingService, you can add the following line for getting the data in the existing onMessageReceived method like this:
 @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
       //...
        Map<String, String> data = remoteMessage.getData();
       //...
    }

Have fun!

I just switched from Parse platform to Firebase and found out that the push notification feature in Firebase replaces Parse very well and has much more room for exploration. It is not hard to learn and integrate to your code. The best part is that it is a free service 😃

If you haven't read the first part of this article, please read it here

Discover and read more posts from Yan Zhang
get started
post commentsBe the first to share your opinion
Mahabub
7 years ago

nice post but if firebase handle the push notification send and receive why we should implement serverside code in PHP or .NET :(

Kapil Mohod
6 years ago

exactly , i am appreciate with you mahabub… and if need services then how please tell us

Alexey Kononov
7 years ago

(っ◔◡◔)っ Wow - thanks!

Muhammad Usman
7 years ago

Cool Work Thank U so Much … :) really awesome and cool :)

Show more replies