capacitor-plugins icon indicating copy to clipboard operation
capacitor-plugins copied to clipboard

Get push notification that open the app from cold boot

Open kevinsimper opened this issue 1 year ago • 6 comments

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.

kevinsimper avatar Mar 15 '23 16:03 kevinsimper

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 {

kevinsimper avatar Mar 16 '23 12:03 kevinsimper

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

kevinsimper avatar Mar 16 '23 12:03 kevinsimper

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).

jswilliams avatar Mar 23 '23 16:03 jswilliams

@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.

joshstrange avatar Aug 08 '23 20:08 joshstrange

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.

joshstrange avatar Aug 10 '23 15:08 joshstrange

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.

dmitry-salnikov avatar Jan 29 '24 14:01 dmitry-salnikov