Firebase Cloud Messaging (FCM) Wrapper for Android

Overview

Pushmanager

minSDK 16 targetSDK 28

Pushmanager is a small wrapper for FCM (Firebase Cloud Messageing) and your app needs just a few methods to interact with it.

Installation

Add google-services as dependency to your project build.gradle

buildscript {
    ...
    dependencies {
        ...
        classpath "com.google.gms:google-services:4.3.5"
        ...
    }
}

and add jitpackto your repositories

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
    ...
}

Add this dependency to your app build.gradle and apply the plugin at the bottom:

implementation 'com.github.grumpyshoe:android-module-pushmanager:1.3.1'
...
apply plugin: 'com.google.gms.google-services'

Usage

Get instance of PushManager:

val pushmanager: PushManager = PushManagerImpl  

Put your google-services.json to the app-root folder.

Create a class extending PushmanagerMessagingService and implement handleNotificationPayload. Here you can decide if a notification should be silent handled or shown to the user according to it's reason.

class MyService : PushmanagerMessagingService() {

    override fun handleNotificationPayload(context:Context, remoteMessageData: RemoteMessageData): NotificationData? {

      Log.d("PushManager", "handlePayload - ${remoteMessageData.title} - ${remoteMessageData.body}" )

      // create pending intent (example)
      val notificationIntent = Intent(context, SomeActivity::class.java)
      notificationIntent.putExtra("info", "Some information for pending intent")
      notificationIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
      val contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)

      // create notification (example)
      return NotificationData(
            context = context,
            title = remoteMessageData.title ?: "Default Title",
            message = remoteMessageData.body ?: "Default Message",
            channelId = "channel_global_notifications",             // needed SDK >= Android O
            autoCancel = true,
            pendingIntent = contentIntent)
    }
}

Add your implementation to you Manifest.xml

<application ... >

       <service
           android:name=".MyService">
           <intent-filter>
               <action android:name="com.google.firebase.MESSAGING_EVENT"/>
           </intent-filter>
       </service>
       ...

</application>

Register to FCM

Call the method register in your onCreate to register to FCM. Notice that register is a coroutine so you have to use something like GlobalScope.launch to run this function.

pushmanager.register(
      context = this,
      onTokenReceived = { token ->
          Log.d("PushManager", "token received: $token")
      },
      onFailure = { exception ->
          Log.d("PushManager", " error during registration: ${exception?.message}")
      })

Unregister from FCM

To unregister from FCM you need to register first. Notice that unregister is a coroutine so you have to use something like GlobalScope.launch to run this function.

pushmanager.unregister(context)

Topics

Subscribe to Topic

To subscribe to a topic just call subscriptToTopic:

pushmanager.subscriptToTopic(
      topic = "wurst",
      onSuccess = {
          Log.d("PushManager", "successfully subscribed")
      },
      onFailure = { exception ->
          Log.d("PushManager", " error while subscribing: ${exception?.message}")
      })

Unsubscribe from Topic

To unsubscribe from a topic call unsubscriptFromTopic:

pushmanager.unsubscriptFromTopic(
      topic = "wurst",
      onSuccess = {
          Log.d("PushManager", "successfully unsubscribed")
      },
      onFailure = { exception ->
          Log.d("PushManager", "error while unsubscribing: ${exception?.message}")
      })

Dependencies

Package Version
com.google.firebase:firebase-core 17.2.0
com.google.firebase:firebase-iid 20.0.0
com.google.firebase:firebase-messaging 20.0.0

Sample App

To run the sample App, just replace the application_id at the project build.gradle with someone according to your firebase project and add your google-services.json to to app root folder.

Troubleshooting

See Troubleshooting at github wiki.

Need Help or something missing?

Please submit an issue on GitHub.

Changelog

See CHANGELOG for morre information.

License

This project is licensed under the terms of the MIT license. See the LICENSE file.

Other information

Build Environment

Android Studio 4.1.2
Build #AI-201.8743.12.41.7042882, built on December 20, 2020
Runtime version: 1.8.0_242-release-1644-b3-6915495 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.15.5
You might also like...
E-commerce android app using firebase and kotlin
E-commerce android app using firebase and kotlin

RSHLN_App e-commerce android app using firebase and kotlin. Images -

FirebaseAuthentication - Login/Register Android Application using Firebase Authentication

FireBaseAuthentication This is a Firebase Authentication Application which will

Post It is the android App for uploading posts and liking them using Firebase and FireStore.

Post-it Post It is the android App for uploading posts and liking them using Firebase and FireStore. This app also uses DAO, RecycleView, Coroutines a

Firebase Authentication and realtime database implementation in Android Kotlin

Androidfirebaseauthentication Firebase is Google’s mobile platform that helps you develop high-quality apps and provides hosted backend services such

To learn how to build an e-commerce app for Android using the Firestore database from Google Firebase Technology

It is an adjustable e-commerce application that you can use to create your own online store or use it as a template to create an e-commerce app for your client. In this app we are covering such topics as Firebase basics how to upload and download data to and from an online database Displaying Images from the Cloud Creating User Profiles Uploading and displaying Products Building a Cart System Selecting images from your phone

FireApp is an open-source project that is built around Firebase Products, especially for learning purposes
FireApp is an open-source project that is built around Firebase Products, especially for learning purposes

FireApp is an open-source project that is built around Firebase Products, especially for learning purposes. This application is written entirely in Kotlin using Android Architecture Components and MVVM architecture pattern. You'll see in the code of this repo, how Firebase Products are working together.

Book Parking is a demo application based on MVVM architecture. The app allows users to booking parking slots, the app uses firebase for the backend.
Book Parking is a demo application based on MVVM architecture. The app allows users to booking parking slots, the app uses firebase for the backend.

Book Parking is a demo application based on MVVM architecture. The app allows users to booking parking slots, the app uses firebase for the backend.

WatchStore Ecommerce app using MVVM, Kotlin, Firebase Firestore, Coroutines, Dagger Hilt
WatchStore Ecommerce app using MVVM, Kotlin, Firebase Firestore, Coroutines, Dagger Hilt

WatchStore Sanple App Like Ecommerce App Developed using Kotlin. Features used: Firebase Firestore Navigation Component. MVVM Architecture RecyclerVie

Google map location tracker uploading current to realtime database and displaying location information from firebase realtime.

WEEK-8-SQ009-ANDROID LOCATION - GROUP WORK (2) Problem Description Track your partner(s). Implementation 1: You are to build a map application to show

Owner
Thomas Cirksena
Passionate android developer since Android 1.5 ..... and still happy with it ;)
Thomas Cirksena
A messaging social platform based on Discord for Desktop and Android.

AP-FinalProject A secure messaging social platform based on Discord for Desktop and Android. This is my final project for Advanced-Programming (AP) in

AmirHosseinAghajari 10 Oct 1, 2022
A simple, secure and instant messaging app. It's cloudbased and free.

Hasten A simple, secure and instant messaging application. It's cloudbased and free. Notice I'm probably at school right now and can't do much ?? . I'

Sliver Hywel 2 Sep 2, 2022
MEGA Android Client - A fully-featured client to access your Cloud Storage provided by MEGA

A fully-featured client to access your Cloud Storage provided by MEGA. This document will guide you to build the application on a Linux machine with Android Studio.

Mega Limited 1.1k Jan 3, 2023
Simple Android app during a coding night. Just Learning Firebase and Android

KUI-App Simple Android app during a coding night. Just Learning Firebase and Android What we learned: Some basics of Android Basic setup of Firebase:

Kibabii University Informatics Club (KUI) 7 Aug 28, 2022
AptiBit is an android application that uses Firebase firestore to store the questions and categorize different types of aptitude questions into their categories

AptiBit is an android application that uses Firebase firestore to store the questions and categorize different types of aptitude questions into their categories. It also uses firebase authentication service that allows you to sign in to the app using your custom credentials.

Ashish Gupta 3 Jun 13, 2022
Clone of real world Chatting application Whatsapp built on Android Studio and Firebase

Clone of real world Chatting application Whatsapp built on Android Studio and Firebase

Aditya Bonde 11 May 23, 2022
✨ Social network app made with Android Compose, full Kotlin, Firebase Authentication, Storage and Firestore 🚀

Your friends, for real. Share daily random moments with your friends and discover who they really are. ✨ Instant Instant is a social media actually un

Vincent Guillebaud 58 Dec 23, 2022
A android plant shop app with firebase backend

PlantShop A android plant shop app with firebase backend What i learned: Create a responsive UI with ConstraintLayout Store product and save to user c

Nguyen Quoc Hung 2 Oct 4, 2022
Projeto de Prova Semestral. Aplicativo Android com login e registro utilizando Firebase Authentication e consumo de API com Retrofit.

Ocean-Tech-Android Projeto de Prova Semestral. Aplicativo Android com login e cadastro utilizando Firebase Authentication e consumo de API com Retrofi

Marcos Lopes da Silva Junior 0 Nov 28, 2021
A Modular Firebase plugin (Android) for godot

GDFirebase GDFirebase is a Modular Godot Plugin for using Firebase Depends on Godot game engine: git clone https://github.com/godotengine/godot Avail

FrogSquare 10 Dec 9, 2022