capacitor
capacitor copied to clipboard
bug + fix: Push Notification handlers not called after webViewWebContentProcessDidTerminate
Bug Report
In our ios app we found that a click on a push message sometimes doesn't execute the javascript push message handlers. We found out that this happens after the webview process has been shut down in background (see also https://github.com/ionic-team/capacitor/discussions/5488 and https://github.com/ionic-team/capacitor/issues/2379) which may happen due to memory pressure. This shutdown calls webViewWebContentProcessDidTerminate
which reloads the app. After this reload, push notification handlers aren't executed.
This issue can be reproduced reliably (see code reproduction below).
Update: I now think this problem isn't specific to the push notifications plugin. I think it's a general problem caused by not clearing plugins' event listeners after reloading the webview. See my comment for a suggested fix.
Capacitor Version
💊 Capacitor Doctor 💊
Latest Dependencies:
@capacitor/cli: 5.0.5
@capacitor/core: 5.0.5
@capacitor/android: 5.0.5
@capacitor/ios: 5.0.5
Installed Dependencies:
@capacitor/android: not installed
@capacitor/cli: 5.0.5
@capacitor/core: 5.0.5
@capacitor/ios: 5.0.5
[success] iOS looking great! 👌
Platform(s)
ios
Current Behavior
After webViewWebContentProcessDidTerminate
, a tap on a push message just opens the app but doesn't call pushNotificationActionPerformed
.
Expected Behavior
After app-reload following webViewWebContentProcessDidTerminate
, push messages that were delivered to the native layer in the meantime should be replayed such that the web app may handle them.
Code Reproduction
- Clone https://github.com/hermitdemschoenenleben/pushmsgtester
- Run it in iPhone simulator
- Go the ios homescreen in order to view incoming push messages
- Drag and drop the file
test-push-message.apns
to the simulator to simulate a push message - Click on the notification -> the app opens and an alert shows: push message click was recognized by the app
- Now, go to the ios homescreen again
- On your mac, execute
kill $(pgrep -P $(pgrep launchd_sim) 'com.apple.WebKit.WebContent')
to kill the webview process (taken from https://stackoverflow.com/a/73609765/5895643 ). This simulates what happens under memory pressure. - Drag and drop
test-push-message.apns
to the simulator again - Click on the notification -> the app opens. This time, no alert is shown, though, as the push message handler is not called after a webview crash
Additional Context
- https://github.com/ionic-team/capacitor/discussions/5488
- https://github.com/ionic-team/capacitor/issues/2379
- https://github.com/ionic-team/capacitor/issues/6153