pythonandroidpush-notificationnotificationsbeeware

How to access Android notifications with a Toga app


I am trying to build an app with Toga/Beeware, mainly for Android but ideally cross-platform. I need the app to send push notifications at user-specified times. I see some have previously attempted the same challenge, but there's currently no official documentation. Has anyone accomplished this? And if so, how?


Solution

  • This page has an example of calling the Android notification API from Python:

            from android.content import Context
            from androidx.core.app import NotificationCompat
    
            builder = NotificationCompat.Builder(activity, App.DEFAULT_CHANNEL)
            builder.setSmallIcon(R.drawable.ic_launcher)
            builder.setContentTitle(
                activity.getString(R.string.demo_notify_title))
            builder.setContentText(
                activity.getString(R.string.demo_notify_text))
            activity.getSystemService(Context.NOTIFICATION_SERVICE)\
                .notify(0, builder.getNotification())
    

    Notes:

    Unfortunately, if your targetSdkVersion is 33 or higher, which is now the case with the default BeeWare Android template, you'll need to ask permission before showing a notification, and I don't have a Python example of that.

    targetSdkVersion 33 or higher will be required by Google Play starting in August 2023. But if you're not going to distribute your app on Google Play, you can work around this issue by using the build_gradle_extra_content option:

    build_gradle_extra_content = "android.defaultConfig.targetSdkVersion 32"