Skip to content

About

Keeps the notification tap that launches a Kivy app, for pyios-firebase (Swift package)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

pyios-firebase-notifications-hook

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.

The problem it solves

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.

Add it to your app

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"

Who needs it

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

How it works

  • It wraps application:didFinishLaunchingWithOptions: of SDL's app delegate class (SDLUIKitSceneDelegate in SDL 3, SDLUIKitDelegate in SDL 2), calling SDL's own afterwards, and also observes UIApplicationDidFinishLaunchingNotification for 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 UNNotificationResponse objects as they are, so pyios-firebase builds the same Notification (title, body, data, identifier, date) as for any other tap.
  • +status, +takeResponses and +version are 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.

License

MIT.

About

Keeps the notification tap that launches a Kivy app, for pyios-firebase (Swift package)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages