Git Product home page Git Product logo

in-app-notification's Introduction

In-app Notification

Library for showing in-app notification in any activity. This library helps you overcome the requirement of show in drop down notification when user is using app since you might have to maintain the legacy application architecture, which is not a single Activity app as well as you do not want to ask your user permission android.permission.SYSTEM_ALERT_WINDOW.

Demo

in_app_notification_demo.gif

Getting started

Add it in your root build.gradle at the end of repositories:

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

In your app build.gradle:

dependencies {
	implementation 'com.github.thuannv:in-app-notification:1.0.0'
}

Sample usage to show notification

First, you must call init() on application onCreate()

class SampleApp: Application() {

    override fun onCreate() {
        super.onCreate()
        ...
        InAppNotificationManager.init(this)
        ...
    }
}

Abstract notification information class

abstract class InAppNotificationInfo(
    val context: Context? = null,
    val autoDismiss: Boolean = false,
    val autoDismissMillis: Long = 0
) {

    abstract fun getView(): View?
}

A discrete implementation notification information

class NormalNotificationInfo(
    private val title: String,
    private val description: String,
    context: Context? = null,
    autoDismiss: Boolean = false,
    autoDismissMillis: Long = 0,
) : InAppNotificationInfo(context, autoDismiss, autoDismissMillis) {

    override fun getView(): View? {
        return context?.let { ctx ->
            ctx.layoutInflater()
                .inflate(R.layout.layout_notification, FrameLayout(ctx), false)
                .also { view ->
                    view.findViewById<TextView>(R.id.first_line_text)?.text = title
                    view.findViewById<TextView>(R.id.second_line_text)?.text = description
                    view.findViewById<ImageView>(R.id.icon)?.apply {
                        Glide.with(this)
                            .load(R.drawable.ic_account)
                            .override(dp(64f), dp(64f))
                            .into(this)
                    }
                    view.setOnTouchListener { _, event ->
                        event?.let { ev ->
                            when (ev.action) {
                                MotionEvent.ACTION_UP -> Log.e("NormalNotification", "up")
                                MotionEvent.ACTION_DOWN -> Log.e("NormalNotification", "down")
                            }
                            true
                        } ?: false
                    }
                }
        }
    }
}

Show the notification

val notification = NormalNotificationInfo.Builder()
      .title("Notification")
      .description("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.")
      .context(context)
      .autoDismiss(true)
      .autoDismissMillis(15000L)
      .build()

InAppNotificationManager.notify(notification)

License

Copyright (C) 2020 thuannv

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

in-app-notification's People

Contributors

thuannv avatar

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.