Tuan Azzam
Tuan Azzam

Reputation: 15

Push notifications in kivy

I'm planning to use kivy to develop an app related to one of my project. But still I couldn't find a way to make the app run in background and while a certain condition is satisfied I need it to display a push notification in mobile. How can I add push notification feature in kivy? Please let me know if someone know how to make it work. (Note - Even the app is closed in mobile it should run in background..).

Upvotes: 1

Views: 2389

Answers (3)

Fabian Joseph
Fabian Joseph

Reputation: 38

I know this is an old question, but in case anyone else is looking for a way background tasks and send push notifications in Kivy.

I’ve implemented this in my own Kivy app to run background tasks like say uploading files while the app is closed. You'll need to create a sticky service. For reference use the Python for Android docs, here's a step-by-step guide:

  1. Create a file ./services/foreground.py

  2. Add the following to your buildozer.spec file:

  • services = Sendnoti:./services/foreground.py:True

  • requirements = python3,kivy,pyjnius

  1. Start the service in your main.py:
# main.py
def startService():
    from jnius import autoclass
    mActivity = autoclass('org.kivy.android.PythonActivity').mActivity
    context = mActivity.getApplicationContext()
    SERVICE_NAME = str(context.getPackageName()) + '.Service' + 'Sendnoti'
    # will be package.domain+package.name+"Service"+"service_mame"
    service = autoclass(SERVICE_NAME)
    service.start(mActivity, '')
    print('Service running in background...')

from kivy.utils import platform
if platform == 'android':
    startService()

Inside your service file, Your condition can be a for or while loop to keep it alive:

# ./services/foreground.py
import time
print("Entered Sendnoti Service is running...")

for progress in range(0, 101, 5):
    time.sleep(5)
    print("python is running...")

For push notifications, you can log updates using adb logcat | grep python. If you prefer a visible notification, you can use a notification library. I built one that makes this easier: android_notify. Here's an example:

# ./services/foreground.py
from android_notify import Notification

notification = Notification(
    title="Downloading...", message="0% downloaded",
    progress_max_value=100, progress_current_value=0,
    style="progress"
)
notification.send()

for progress in range(0, 101, 5):
    time.sleep(5)
    notification.updateProgressBar(progress, f"{progress}% downloaded")

Upvotes: 0

Inaimathi
Inaimathi

Reputation: 14065

There is a subsection of the plyer library that deals with notifications. The example implies that you can do

import plyer

plyer.notification.notify(
    title="Test Title",
    message="Test Message",
    ticker="Test Ticker",
    app_name="YourAppNameHere"
)

Upvotes: 0

just a stranger
just a stranger

Reputation: 320

I can't tell you how to keep running in the background (because I don't know either) but I can show you how to notify.

by the way,plyer doesn't work as well as windows on android so you should throw with java lib jnius

codes is here:

from kivy.app import App
from kivy.uix.button import Button
from jnius import autoclass

def notify(*args):
    AndroidString = autoclass('java.lang.String')
    PythonActivity = autoclass('org.kivy.android.PythonActivity')
    NotificationBuilder = autoclass('android.app.Notification$Builder')
    Context = autoclass('android.content.Context')
    Drawable = autoclass('org.test.notify.R$drawable')
    icon = Drawable.icon
    notification_builder = NotificationBuilder(PythonActivity.mActivity)
    notification_builder.setContentTitle(AndroidString('Title'.encode('utf-8')))
    notification_builder.setContentText(AndroidString('Message'.encode('utf-8')))
    notification_builder.setSmallIcon(icon)
    notification_builder.setAutoCancel(True)
    notification_service = notification_service = PythonActivity.mActivity.getSystemService(Context.NOTIFICATION_SERVICE)
    notification_service.notify(0,notification_builder.build())

class NotifyApp(App):
    def build(self):
    return Button(text="notify", on_press=notify)

if __name__ == '__main__':
    NotifyApp().run()

if you keep getting errors,look here

Upvotes: 1

Related Questions