capacitor-plugins
capacitor-plugins copied to clipboard
Get push notification that open the app from cold boot
Feature Request
Plugin
push notification
Description
When the app launches from a push notification from cold, there is no way of reaching that message in capacitorjs
Platform(s)
ios
Preferred Solution
It looks like it is possible to reach with
UIApplicationLaunchOptionsRemoteNotificationKey
https://developer.apple.com/documentation/uikit/uiapplicationlaunchoptionsremotenotificationkey
Alternatives
Additional Context
It make sense that the addEventListener for pushNotificationReceived
and pushNotificationActionPerformed
can be called if they have not been added.
But also getDeliveredNotifications()
is empty if the user clicked the push notification that then launched the app
None of the getState() or getLaunchUrl() indicates the push notification that triggered the app start either.
I digged more into iOS and I found that you can find push notification with this
if let userInfo = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: AnyObject] {
if let aps1 = userInfo["aps"] as? NSDictionary {
print(aps1)
}
}
So inside the
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
I found a way by storing the received push notifaction in the Capacitor Preferences plugin, that I then can receive in my app later 😅 Not very elegant, but it works as a POC for now
Can you post or link your work around solution? I’m encountering the same issue on cold boot (when the WebView was terminated by iOS).
@kevinsimper Could you please share your code to store the data into UserDefaults/SharedPreferences (if you also wrote an implementation for Android). We're running into the same issue and your POC sounds like the route we will take as well.
It's very odd that this plugin doesn't have a PushNotifications.getPushNotificationReceived()
-type function to do this like App
has both App.getLaunchUrl()
and App.addListener('appUrlOpen', ...)
. It feels like a very common use-case to just ignore.
After digging some more and working on a method to use UserDefaults
we discovered, by accident, that the event handler (pushNotificationActionPerformed
) appears to be called on cold start when you start listening. It would be helpful if the docs mentioned this, a lot of other Capacitor plugins have 2 methods to handle cold-start/warm-start events (like the App.getLaunchUrl()
and App.addListener('appUrlOpen', ...)
mentioned above). We missed it initially because our app was not ready to handle the push notification at the time the handler first fired and since debugging is difficult from a cold start we never saw the logs that it was hit.
We were adding the event handlers super early in our app lifecycle so instead we delayed setting up our event handlers until a little later and we started getting the event at a time our app could correctly handle it.
To reiterate: We ARE getting events on cold start in our pushNotificationActionPerformed
handler. It appears that attaching the listener causes the plugin to then fire the push notification that caused the cold start.
In a lot of ways this is nicer than what we proposed above but it's a little unexpected. I hope this helps the next person that finds this ticket.
Thanks @joshstrange,
the event handler (pushNotificationActionPerformed) appears to be called on cold start when you start listening
I can confirm that works, and yes - it has to be properly documented in plugin readme.