Coder Social home page Coder Social logo

fcm-client-lib's Introduction

MavenBadge

Firebase Messaging Client

A library which helps you handle Firebase Cloud Messaging(FCM). This library helps in abstracting out the logic firebase message lifecycle, it takes care of token lifecycle and delivers the notification payload received from FCM to the application via a callback listener. The library internally manages token registration, token refresh and also has a retry mechanism in case of registration failure.

Features

  • FCM token registration
  • FCM token rotation
  • Callback for token registration change
  • Callback for FCM messages
  • Retry mechanism on timeouts or registration failures
  • Fallback for missed token rotation callback
  • Subscribe/un-subscribe to topics

Installation

Add the below line in the dependency block of your app's build.gradle

implementation("dev.assemblage:fcm-client:$sdkVersion")

replace $sdkVersion with the latest version of the SDK

Set-up Firebase

Before initialising the library make sure you have added the required plugins and google-services.json file into your project. Refer to the documentation for more details.

Note: Dependencies and listeners required for the Firebase Messaging to work is already added in the library, you need not add it again.

Initialisation

Initialise the FCM client SDK in the onCreate() of the Application subclass. To initialise add the below line.

FcmClientHelper.getInstance(applicationContext).initialise()

Optionally you can set the log level and retry interval in the initialise API, refer to the API documentation for more details.

Set-up Callback Listener

To receive a callback whenever a token is generated or notification is received implement the FirebaseMessageListener interface and register a listener using the addListener() of the FcmClientHelper. Please ensure this callback is registered in the onCreate() of the Application sub-class of your application. Below is an example of setting up the callback in the onCreate() of Application class

FcmClientHelper.getInstance(applicationContext).addListener(FirebaseListener())

Token callback: onTokenAvailable() is called whenever a new token is available.

Push Payload callback: onPushReceived() is called a push notification is received on the device.

Excluding files from back-up

Auto Backup for Apps automatically backs up a user's data from apps that target and run on Android 6.0 (API level 23) or later. As a general recommendation push tokens should not be backed up as it gets invalidated over time. So to ensure token is not backed up exclude the shared preference file from the back-up.

To exclude the file include the below in your app's backup descriptor

    <exclude
        domain="sharedpref"
        path="dev_assemblage_fcm_client_pref" />

If your application is targeting API level 31 along with the above add the below in the new format back up configuration.

  <cloud-backup>
    <exclude
        domain="sharedpref"
        path="dev_assemblage_fcm_client_pref" />
  </cloud-backup>
  <device-transfer>
    <exclude
        domain="sharedpref"
        path="dev_assemblage_fcm_client_pref" />
  </device-transfer>

If your app does not have a back-up descriptor already you can directly add the back-up descriptor provided by the SDK as shown below

<application>
  android:fullBackupContent="@xml/fcm_client_backup_descriptor"
  android:dataExtractionRules="@xml/fcm_client_data_extraction_rules"
</application>

Topics Messaging

Library supports topic messaging. The app can subscribe/un-subscribe to messages whenever required.

API for subscribing to topic

FcmClientHelper.getInstance(applicationContext).subscribeToTopics()

API for un-subscribing to topic

FcmClientHelper.getInstance(applicationContext).unSubscribeTopic()

Enable logging

By default only info logs are enabled to enable verbose logging pass in the log-level in the initialise() as shown below

FcmClientHelper.getInstance(applicationContext).initialise(this, Logger.LogLevel.VERBOSE)

Refer to the API documentation for more details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.