This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

Localised notifications with Firebase cloud messaging (FCM)


If you have to ship localised push notifications from a Java server aspect software to cell units with Firebase Cloud Messaging (FCM), whether or not your server aspect is Grails, Spring boot or every other Java primarily based framework utilising the Firebase (Admin) SDK then search no extra as it is not so clear within the official documentation. Most examples deal with the REST or JS implementations.

In my use case, I needed to alert customers subscribing to a particular Subject of a selected occasion. Customers of this app are distributed globally so it was essential to localise the messages relying on the customers locale based on their gadget. From the server aspect, I simply wish to generate the notification and let Firebase’s servers deal with the notifications to every gadget subscribed to the subject after which the gadget it self show a locales message, each the title and subtitle, on each Android and IOS units.

Happily Firebase lets us do that by specifying localised ‘keys’, as a substitute of specifying the title and physique. Nonetheless discovering an instance was extra of a battle because the documentation is considerably missing.

What we’ve to do is set up Choices objects for every platform, i.e. one for Android and one for Apple units after which deep inside nested Builders we are able to lastly set the localised keys. These then could be interpreted from the assets inside the cell App itself and might be exhibited to the person, each when the app is within the foreground AND additionally when it is within the background.

So with out additional ado, let’s take a look at the code:

Word: I am omitting the remainder of the app code assuming you already know find out how to setup Firebase and so on and simply have to see find out how to generate the notification.

void sendLocalisedNotification(String subject, String titleKey, String bodyKey) {
        // Set localisation key for Android
        AndroidConfig androidConfig = AndroidConfig.builder()
            .setNotification(
                AndroidNotification.builder()
                    .setTitleLocalizationKey(titleKey)
                    .setBodyLocalizationKey(bodyKey)
                    .construct()
            ).construct()

        // Set localisation key for Apple units
        ApnsConfig apnsConfig = ApnsConfig.builder()
            .setAps(Aps.builder()
                .setAlert(ApsAlert.builder()
                    .setTitleLocalizationKey(titleKey)
                    .setSubtitleLocalizationKey(bodyKey)
                    .construct()
                ).construct()
            ).construct()

        Message message = Message.builder()
            .setTopic(subject)
            .setAndroidConfig(androidConfig)
            .setApnsConfig(apnsConfig)
            .construct();

        // After which ship the message to the units subscribed to the offered subject.
        strive {
            String response = FirebaseMessaging.getInstance().ship(message);

            // Response is a message ID string.
            log.debug("Firebase message $subject despatched: " + response);
        } catch (Exception ex) {
            log.warn("Firebase message $subject failed: " + ex.message);
        }
    }
Enter fullscreen mode

Exit fullscreen mode

So that is the gist of it, this technique could be referred to as with the subject we wish to publish to, the title key and the physique key.

I hope this helps save somebody time discovering find out how to accomplish localised messages below a Java surroundings utilizing the FCM SDK.

Dave / Tucanoo Solutions

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?