pushyy
pushyy copied to clipboard
Simplified Push Notifications with Kivy
pushyy
A Python module meant to simplify working with push notifications in Kivy
Video tutorial: https://youtu.be/8nrXsWeRG8I
Features
- Receive push notifications when your app is in the foreground
- Receive push notifications when your app is in the background or not running
- Run a background function when notification is received when app is not running
- Get a device token
- Listen for device token changes
Credits
-
Flutter Firebase Messaging: The Java classes are from there
- Kindly create a PR correcting the copyright if it's wrong :)
-
Electrum for the onNewIntent
Usage Overview
from pushyy import Pushyy
# Get device token
def my_token_callback(token: str) -> None:
send_to_server(token)
Pushyy().get_device_token(my_token_callback)
# Listen for new device token
def new_token_callback(token: str) -> None:
print(token)
Pushyy().token_change_listener(new_token_callback)
# Get notification data when app is in foreground
def my_foreground_message_callback(notification_data: RemoteMessage) -> None:
print(notification_data)
Pushyy().foreground_message_handler(my_foreground_message_callback)
# Get notification data when user taps on notification from tray
def my_notification_click_callback(notification_data: RemoteMessage) -> None:
print(notification_data)
Pushyy().notification_click_handler(my_notification_click_callback)
See
src/python/main.py
on how the UI is being updated
Background function
To run custom code in the background when a notification is received and your application is not running, write your code in the my_background_callback
function in python_notification_handler.py
def my_background_callback(notification_data: RemoteMessage) -> None:
"""
Note: Application is not visible to the user here
One of the things you can do here: Mark a chat message
as delivered by making a request to your server.
"""
try:
# connect to server
pass
except:
pass
Set up
Part 1
- Clone python-for-android
- Open the file
pythonforandroid/bootstraps/common/build/templates/build.tmpl.gradle
- Add the following:
- Under
buildscript->dependencies
addclasspath 'com.google.gms:google-services:4.3.4'
- Below
apply plugin: 'com.android.application'
addapply plugin: 'com.google.gms.google-services'
- Under
dependencies
addimplementation platform('com.google.firebase:firebase-bom:X.Y.Z')
(replace XYZ with the latest version from here, tested with28.1.0
)
- Under
- Open the file
pythonforandroid/bootstraps/sdl2/build/templates/AndroidManifest.tmpl.xml
- Before the
</application>
tag, add
<service
android:name="org.kivy.plugins.messaging.KivyFirebaseMessagingBackgroundService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false"/>
<service android:name="org.kivy.plugins.messaging.KivyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<receiver
android:name="org.kivy.plugins.messaging.KivyFirebaseMessagingReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</receiver>
- Create a Firebase project here
- Add an Android app and skip the steps since we already did that at
- Download the
google-services.json
- Move it to
pythonforandroid/bo -> Noneotstraps/common/build/
folder
Part 2
- Place pushyy.py and python_notification_handler.py next to your
main.py
- Place libs/ in the same folder as
buildozer.spec
- In your
buildozer.spec
find and set:
android.add_src = libs/
android.gradle_dependencies = com.google.firebase:firebase-messaging,com.google.firebase:firebase-analytics,com.google.code.gson:gson:2.8.6
p4a.source_dir = /path/to/cloned/python-for-android
services = PythonNotificationHandler:python_notification_handler.py
# NB: File name must be python_notification_handler.py
-
Open
PlatformIntermediate.java
from yourlibs/
folder and replacecom.waterfall.youtube
withyour.app.packagename
-
Open
KivyFirebaseMessagingBackgroundExecutor.java
from yourlibs/
folder and replacecom.waterfall.youtube
withyour.app.packagename
Notes
- This module is aimed for Android. For iOS, you may consider this video
- Just to clarify, with Plyer you get to show local notifications, not push notifications. Which notifications should i use, Push notification or local notification?
- Why Pushyy? First thing that came to mind
¯\_(ツ)_/¯