A Python module meant to simplify working with push notifications in Kivy
Video tutorial: https://youtu.be/8nrXsWeRG8I
Flutter Firebase Messaging: The Java classes are from there
- Kindly create a PR correcting the copyright if it's wrong :)
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
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
pythonforandroid/bootstraps/common/build/templates/build.tmpl.gradle
buildscript->dependencies
add classpath 'com.google.gms:google-services:4.3.4'
apply plugin: 'com.android.application'
add apply plugin: 'com.google.gms.google-services'
dependencies
add implementation platform('com.google.firebase:firebase-bom:X.Y.Z')
(replace XYZ with the latest version from here, tested with 28.1.0
)pythonforandroid/bootstraps/sdl2/build/templates/AndroidManifest.tmpl.xml
</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>
google-services.json
pythonforandroid/bo -> Noneotstraps/common/build/
folder
main.py
buildozer.spec
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
5. Open `PlatformIntermediate.java` from your `libs/` folder and replace `com.waterfall.youtube` with `your.app.packagename`
6. Open `KivyFirebaseMessagingBackgroundExecutor.java` from your `libs/` folder and replace `com.waterfall.youtube` with `your.app.packagename`
Notes
---------
- This module is aimed for Android. For iOS, you may consider [this](https://youtu.be/mONyhxt2KV8) video
- Just to clarify, with [Plyer](https://github.com/kivy/plyer) you get to show local notifications, not push notifications. [Which notifications should i use, Push notification or local notification?](https://stackoverflow.com/questions/45343427/which-notifications-should-i-use-push-notification-or-local-notification)
- Why Pushyy? First thing that came to mind `¯\_(ツ)_/¯`