Python 在kivy中推送通知

Python 在kivy中推送通知,python,kivy,Python,Kivy,我计划使用kivy开发一个与我的一个项目相关的应用程序。但我仍然找不到一种方法让应用程序在后台运行,当满足一定条件时,我需要它在手机上显示推送通知。如何在kivy中添加推送通知功能?请让我知道,如果有人知道如何使它工作。(注意-即使应用程序在移动设备中关闭,也应该在后台运行。)我不能告诉你如何在后台运行(因为我也不知道),但我可以告诉你如何通知 顺便说一下,plyer在android上的运行不如windows,所以您应该使用java lib jnius 代码如下: from kivy.app i

我计划使用kivy开发一个与我的一个项目相关的应用程序。但我仍然找不到一种方法让应用程序在后台运行,当满足一定条件时,我需要它在手机上显示推送通知。如何在kivy中添加推送通知功能?请让我知道,如果有人知道如何使它工作。(注意-即使应用程序在移动设备中关闭,也应该在后台运行。)

我不能告诉你如何在后台运行(因为我也不知道),但我可以告诉你如何通知

顺便说一下,plyer在android上的运行不如windows,所以您应该使用java lib jnius

代码如下:

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()

如果您不断出现错误,请看

我无法告诉您如何在后台继续运行(因为我也不知道),但我可以告诉您如何通知

顺便说一下,plyer在android上的运行不如windows,所以您应该使用java lib jnius

代码如下:

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()
如果你不断地出错,看看