Push notifications in kivy - python

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

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

Related

How to get a Kivy app on Android to access the Internet

I have a simple Kivy app to try out a clickable link opening a Web site. The code is:
kivy.require('1.9.0')
from kivy.app import App
from kivy.uix.label import Label
import webbrowser
class TestApp(App):
def build(self):
ref = '[ref=https://kivy.org/doc/stable/]' + 'Kivy documentation' + '[/ref]'
link = Label(text=ref, markup=True, on_ref_press=self.OpenLink)
return link
def OpenLink(self, label, ref):
webbrowser.open(ref)
if __name__ == '__main__':
TestApp().run()
When I run this app in the Kivy Launcher on Android, tapping the link works and opens the url in the browser. However, when I run the compiled app nothing happens when I tap the link.
In the buildozer.spec file I have:
android.permissions = INTERNET
According to all information I have been able to find, this is all I should need. What else am I missing?
I have tried logging the app using adb logcat but I can't see anything relating to the problem.

Kivy App crashes when using anything that needs internet

i am new to kivy and would like some help guys .
i am building a Kivy App with buildozer for android , that uses an outsource API from cryptocompare.com
it is a very simple app that shows the Cryptocurrency prices , live updated.
the app crashes instantly when opened ..
i removed the API and created a Dict instead, just to test if the App will work or not , and the app runs perfectly on android .. when i put back the API , it just keep crashing .
note that the app works fine on Pycharm with no error .. also the Buildozer gives no error ..
i've been trying to find out the problem for so long .
tried every possible way . no luck.
almost included everything in android.permissions with no luck also .
this is my app code
from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivymd.uix.list import MDList, TwoLineListItem
from kivy.uix.scrollview import ScrollView
from kivy.network.urlrequest import UrlRequest
class CryptoPrices(MDApp):
def build(self):
screen = Screen()
scroll = ScrollView()
listview = MDList()
scroll.add_widget(listview)
def got_json(req, result):
for x, y in result.items():
name = x
symbol = str(y["USD"]) + " USD"
item1 = TwoLineListItem(text=name, secondary_text=str(symbol))
listview.add_widget(item1)
req = UrlRequest('https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,BCH,LTC,XLM,DASH,XEM,LIBRA,BNB,ETH,XMR,XRP,LTC&tsyms=USD', got_json)
screen.add_widget(scroll)
return screen
if __name__ == '__main__':
CryptoPrices().run()
link to buildozer.specs
https://textuploader.com/1f7ie
link to log file
https://textuploader.com/1f7ii
Two possible issues:
You may need to request runtime permissions. See runtime permissions
You might need to add requests to your requirements in the spec file.
From the buildozer.spec file, you have to set internet permissions.
By default this option is commented, just uncomment it and the app should work fine.
The logfile that you have added is buildozer log when the app is being built. To get the app log when it's running, add the logcat function (buildozer android logcat) In app logs you can find what causing your app to crash any requirements or permission or anything else

How to use Pygubu - Python

I am trying to use pygubu from the first time to make better GUI's. I have installed it using pip and it has installed correctly. However when I try and run the example code Here (At the bottom of the web page) I get the error
AttributeError: module 'pygubu' has no attribute 'Builder'
I don't know if this code is correct or not. I have looked for ways to use this tool but all I can find are links and videos for installing it. I have also tried This video but can't figure out how to open/run/use. I am using python-idle if that is the issue. The code (if you don't want to follow the link) is:
# helloworld.py
import tkinter as tk
import pygubu
class HelloWorldApp:
def __init__(self):
#1: Create a builder
self.builder = builder = pygubu.Builder()
#2: Load an ui file
builder.add_from_file('helloworld.ui')
#3: Create the mainwindow
self.mainwindow = builder.get_object('mainwindow')
def run(self):
self.mainwindow.mainloop()
if __name__ == '__main__':
app = HelloWorldApp()
app.run()
I would appreciate any help. Also when I try installing it again - just to check - I get:
Pygubu is an application. Find it in files from C: drive. The code:
# helloworld.py
import tkinter as tk
import pygubu
class HelloWorldApp:
def __init__(self):
#1: Create a builder
self.builder = builder = pygubu.Builder()
#2: Load an ui file
builder.add_from_file('helloworld.ui')
#3: Create the mainwindow
self.mainwindow = builder.get_object('mainwindow')
def run(self):
self.mainwindow.mainloop()
if __name__ == '__main__':
app = HelloWorldApp()
app.run()
Is run after you have created your UI (The format that this application uses) and that the name of the UI specified is helloworld.ui.
Note that instead of helloworld.ui in the following line:
builder.add_from_file('helloworld.ui') You should insert the filename
(or path) of your just saved UI definition.
Note also that instead of 'mainwindow' in the following line:
self.mainwindow = builder.get_object('mainwindow') You should have the
name of your main widget (the parent of all widgets), otherwise you
will get an error similar to the following:
Exception: Widget not defined.
This link explains the usage better than one stated in question

python: kivy app not closing

my kivy simple hello world app is not closing I'm using raspberry pi B and I can't close it I must unplug my raspberry pi 5v adapter to close it
I'm using rasbian jessie
this is the very simple code
import kivy
from kivy.app import App
from kivy.uix.label import Label
class mamdouh(App):
def build(self):
return Label(text='mamdouh')
if __name__=='__main__':
mamdouh().run()
What I was trying to say in the comment was. That you need to have some action to cause the quit, as the run method will run in a loop indefinitely otherwise.
If you now click on the Button labeled 'paul' it will quit.
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
class mamdouh(App):
def build(self):
lbl = Label(text='paul')
btn = Button(text='mamdouh')
btn.bind(on_press=lambda b: app.stop())
lbl.add_widget(btn)
return lbl
if __name__ == '__main__':
app = mamdouh()
app.run()
I know nothing about kivy but I can see that this should allow you to quit, whether you want a Button in your app is another question.
Another way to kill it and avoid the reboot is simply to go back to the prompt where you launched your app and do CTRL + C. This will only work from the prompt though, not from the app window itself.

Finding GUI elements with pywinauto

I am trying my first things with pywinauto.
Now I want to make use of print_control_identifiers() but I get errors, however I write my code - I cant get any information about GUI objects.
I already tried to generate the code via swapy - had a lot of generated code, but no success.
This is my code so far:
import getpass, fnmatch
from pywinauto import application
currentUser = getpass.getuser()
if fnmatch.fnmatch(currentUser, "axe"):
pwa_app = application.Application()
w_handle = application.findwindows.find_windows(title=u'Login - 0.9.347', class_name='WindowsForms10.Window.8.app.0.141b42a_r11_ad1')[0]
window = pwa_app.window_(handle=w_handle)
window.SetFocus()
ctrl = window['Log In']
ctrl.Click()
else:
print "You need admin rights for that action"
Can you tell me, where I need to use print_control_identifiers()?
Do you have any other GUI automation frameworks that are more up-to-date?
PrintControlIdentifiers() is useful for top level window. If window is top level window specification, then just call
window.PrintControlIdentifiers()
or
pwa_app.Window_(title=u'Login - 0.9.347', top_level_only=True).PrintControlIdentifiers()
A few examples:
Swapy is good to identify the properties. Also, the examples given with pywinauto are quite helpful.
Source: https://pywinauto.googlecode.com/hg/pywinauto/docs/getting_started.html
from pywinauto import application
app = application.Application.Start("Notepad.exe")
app.Notepad.print_control_identifiers()
app.Notepad.MenuSelect("Edit->Replace")
app.Replace.print_control_identifiers()
from pywinauto import application
from pywinauto import application
app = application.Application()
app.Start("Notepad.exe")
Wnd_Main = app.window_(title_re=".*Notepad")
Wnd_Main.MenuSelect("File->Save")
Wnd_Save = app.window_(title_re="Save As")
Wnd_Save.Edit1.SetEditText("Hello World.txt")

Categories