A tiny Swift package (one Objective-C file) that lets a Kivy app built with pyios-firebase receive the notification tap that launches the app.
When the user taps a notification while your app is not running, iOS launches
the app and delivers the tap during launch. In a Kivy app, SDL starts Python
only after launch, so there is no notification handler yet: iOS drops the tap
(response-not-possible), and your on_open never runs. The app opens, but
does not know which notification was tapped.
This package's +load runs before Python. It makes sure a notification-center
delegate is set before launch ends, keeps the taps it receives, and
pyios-firebase's notifications.set_handlers(on_open=...) passes them to your
on_open, as soon as your app's event loop runs.
Nothing else changes: once Python is running, set_handlers installs its own
handler, as it always does.
kivyforge (pyproject.toml):
[tool.kivy.ios.native.swift_packages.PyiosFirebaseNotificationsHook]
url = "https://github.com/PySimpleObjC/pyios-firebase-notifications-hook.git"
requirement = { from = "0.1.0" }
products = ["PyiosFirebaseNotificationsHook"]Xcode (kivy-ios):
File > Add Package Dependencies..., enter
https://github.com/PySimpleObjC/pyios-firebase-notifications-hook.git, and
add the PyiosFirebaseNotificationsHook product to the app target.
Then, in Python, nothing new: call set_handlers early, before App.run():
from pyios_firebase.messaging import notifications
notifications.set_handlers(on_open=lambda notification, action: go_to(notification.data))
print(notifications.launch_hook_status()) # "ok: SDLUIKitSceneDelegate"| App | Needs it? |
|---|---|
| Kivy (SDL), and you want to know which notification launched the app | Yes |
| Kivy, notifications only while the app is open or in the background | No: those already reach on_open |
| BeeWare / Toga | No: Python starts before launch there |
- It wraps
application:didFinishLaunchingWithOptions:of SDL's app delegate class (SDLUIKitSceneDelegatein SDL 3,SDLUIKitDelegatein SDL 2), calling SDL's own afterwards, and also observesUIApplicationDidFinishLaunchingNotificationfor any other app. Either way, the delegate is set before iOS delivers the launching tap. - It never replaces a delegate the app set itself.
- It keeps the
UNNotificationResponseobjects as they are, so pyios-firebase builds the sameNotification(title, body, data, identifier, date) as for any other tap. +status,+takeResponsesand+versionare its whole interface; Python reads them through Rubicon-ObjC.
Tested end to end in pyios-firebase's iOS Simulator suite: the app is closed,
a notification is pushed with xcrun simctl push, the banner is tapped, and
on_open gets the notification's data.
Based on the idea of kivyforge's ios-lifecycle-hook example (MIT), which hooks SDL's delegate the same way for several app events.
MIT.