Coder Social home page Coder Social logo

patilshreyas / fcm-ondevicenotificationscheduler Goto Github PK

View Code? Open in Web Editor NEW
116.0 7.0 28.0 2.31 MB

Demo implementation to Schedule FCM Notifications on Android Device using AlarmManager + WorkManager.

License: MIT License

Kotlin 100.00%
fcm-notifications fcm android workmanager alarmmanager kotlin-android kotlin firebase firebase-cloud-messaging

fcm-ondevicenotificationscheduler's Introduction

πŸ”” FCM - Push Notification Scheduler⏰ (On Device πŸ“±)

Article About this is available here.

This is demo app to implement FCM On Device Push Notification Scheduling using AlarmManager and WorkManager.

Show some ❀️ and star the repo to support the project

GitHub stars GitHub forks GitHub watchers GitHub followers Twitter Follow Medium Article License

Introduction

  • We can use Cloud Pub/Sub with Firebase Cloud Functions to send FCM Push Notifications. But this solution is costly to implement.
  • Here, We are using regular FCM Push Notifications and scheduling it on device.

In this demo app, We are subscribing to a FCM Channel discount-offers. We'll receive Data payload from FCM to this subscribed channel and we will process and schedule it.

Dependencies

  • Firebase Messaging - Firebase Cloud Messaging Library.
  • Firebase IID - Firebase Instance ID Library.
  • WorkManager - Used for Background Work Processing.

Setup

  • Setup project in Firebase Console.
  • Download and paste google-services.json configuration file in /app directory.
  • Keep legacy API key for FCM Push Notification REST.

Implementation Structure

src
β”‚
└───fcm
β”‚   β”‚   MyFirebaseMessagingService.kt
β”‚   β”‚   NotificationBroadcastReceiver.kt
β”‚   β”‚   ScheduledWorker.kt
β”‚   
└───util
|   β”‚   NotificationUtil.kt
|   β”‚   SettingUtil.kt
|
└───ui
|   β”‚   MainActivity.kt
|   
  • MyFirebaseMessagingService: FCM Receiver Service Implementation. Process of Notification Scheduling using AlarmManager is implemented here.

  • NotificationBroadcastReceiver: BroadcastReceiver Implementation. Executed when AlarmManager is triggered. WorkManager is initiated and executed for background processing.

  • ScheduledWorker: WorkManager Implementation. Notification is displayed in the system tray and other background processed are executed.

  • NotificationUtil: Implementation to display Notification on the system tray.

  • SettingUtil: Function implementation to check whether Automatic Date & Time setting is ON/OFF.

  • MainActivity: UI Implementation to subscribe to FCM Channel.

What's Happening? πŸ€”

  • Subscribe to discount-offers FCM Channel from Android Device.
  • Data Payload will be as follows
 { 
    "to": "/topics/discount-offers", 
    "priority": "high",
    "data" : {
      "title" : "TITLE_HERE",
      "message" : "MESSAGE_HERE",
      "isScheduled" : "true",
      "scheduledTime" : "2019-12-13 09:41:00"
    }
}

Format of scheduledTime: YYYY-MM-DD HH:MM:SS

  • Receive FCM on device and onMessageReceived() in MyFirebaseMessagingService will be invoked. In this, following operations will be done-
    • If isScheduled parameter received is false then notifications is displayed in system tray instantly.
    • If isScheduled is true then scheduledTime is parsed from payload and AlarmManager is used to set one-time alarm at that time and NotificationBroadcastReceiver implementation will be executed on that time.
    • In onReceive(), we have scheduled a WorkManager ScheduledWorker for background work processing. There in doWork(), we're finally we're displaying Notification on system tray. Do any background proessing and return status from WorkManager.

Hurrah!😍 we have successfully implemented On-Device Scheduling of FCM Push NotificationπŸ‘.

Let's Test It

I have sent below payload with to the FCM Channel (discount-offers).

{ 
 "to": "/topics/discount-offers", 
 "priority": "high",
 "data" : {
  "title" : "πŸŽ… Christmas Offer πŸŽ„",
  "message" : "Grab 90% Discount 😍 on Mobile Phones",
  "isScheduled" : "true",
  "scheduledTime" : "2019-12-13 14:12:00"
 }
}

πŸš€See output below and notice that Internet/Wi-Fi is OFF still at exactly 02:12 pm I'm getting a notification on the system tray πŸ˜ƒ.

Yippie 😍! It's working as expected. Hope you liked that. If you find it helpful please share this. Maybe it'll help someone needy!

❀️ Found this project useful?

If you found this project useful, then please consider giving it a ⭐ on Github and sharing it with your friends via social media.

Sharing isΒ Caring!

Connect With Me

If you want to contact me, feel free to reach me… Visit My Profile.

License

MIT License

Copyright (c) 2019 Shreyas Patil

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

fcm-ondevicenotificationscheduler's People

Contributors

patilshreyas avatar samtstern avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

fcm-ondevicenotificationscheduler's Issues

Possible Using Postman ?

i'm working on alarm Api, here i need to send notification on Perticuler time it is not working ? it is possible Using Api ?

alarmManager.set does not work

Hello, I have tried using alarManager.set, but it does not work with all devices, particularly with Huawei or Xiaomi. I tried to use setExactAndAllowWhileIdle, but it also does not work if app is closed. What are your suggestions?
Will you update your sample to tackle these issues?

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.