GithubHelp home page GithubHelp logo

Comments (2)

AtjonTV avatar AtjonTV commented on July 25, 2024

Here is a little bit of Kotlin source code from my app (https://billstar.app).

I confirmed that code to work on API 19 up to 30. You will either have to add Kotlin to your project, or translate it to Java.

The createNotificationChannel has to be called during the creation of your main activity and wrapped in a Version check, the sendNotification can then be called from anywhere.

import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import android.app.NotificationChannel
import android.app.NotificationManager
import androidx.annotation.RequiresApi
// Here might be some more androidx imports missing

object Notify {
    private val channel = "UPDATE"
    private val channelDescription = "Get all updates to events"
    private var notifyId = 1

    fun sendNotification(title: String, text: String) {
        val mBuilder =
            NotificationCompat.Builder(context, channel)
                .setContentTitle(title)
                .setContentText(text)
                .setPriority(NotificationCompat.PRIORITY_LOW)
                .setAutoCancel(false)

        val notificationManager = NotificationManagerCompat.from(context)
        notificationManager.notify(notifyId++, mBuilder.build())

    }
    
    @RequiresApi(Build.VERSION_CODES.O)
    fun createNotificationChannel() {
        // Create a new Notification Channel (Android 8 [API 26] and later)
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val channel = NotificationChannel(channel, channel, importance).apply {
            description = channelDescription
        }
        // Register the channel with the system
        val notificationManager: NotificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }
}

As you dont need a channel on pre O android, it should "just work". My app will drop support for API pre 21, but 21 is your target too anyways.

As my app is closed source, I had to strip some stuff our, if that code above does not work I am happy to help fix what I messed up during stripping.

from snapdrop-android.

fm-sys avatar fm-sys commented on July 25, 2024

@AtjonTV thanks for providing this information! I will take a deeper look into it in the next few days, and may come back to you if I don't get it working...

from snapdrop-android.

Related Issues (20)

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.