Cordova detect cold start from Push Notification
Asked Answered
J

2

6

I have a cordova app (codova (3.4.0) running on iOS and soon also on Android. Push notifications are implemented and working. I'm having troubles detecting when the app was started through a push notification and redirecting the application to the right page.

Note: This doesn't concern starting the app from a push notification when the app is running in the background. Only when the app is completely closed!

I now have the following workflow:

Regular startup:

  • Wait for cordova.js to fire the ondeviceready event.
  • In the ondeviceready event redirect to the startup view of my app ( via window.location.hash)

Cold app startup from push notification:

  • Wait for cordova.js to fire the ondeviceready event.
  • In the ondeviceready event redirect to the startup view of my app (via window.location.hash)
  • Plugin kicks in and triggers a javascript function in my app called notificationreceived
  • In notificationreceived function redirect to a view based on the push notification

As you can see the cold app startup scenario going to the startup view, which is unnecessary and causes the user to wait for the first view to load, only to be redirected again.

The problem is that when deviceready fires, my javascript code isn't aware of the coming push notification yet, so i'm looking for a way around this.

Is there a way to maybe pass extra parameters to the deviceready cordova event? Or does anybody have another idea or solution to tackle this problem?

Julijulia answered 21/10, 2014 at 8:46 Comment(2)
Hi Jermaine, i wonder if you have found how to workaround this problem. Right now I check my server to download fresh data during startup and this creates a second notification for the same event. I have looked at the source of the plugin and it seems to send some extra parameters for our cordova apps to start (), though I'm not really sure if these parameters finally arrive to the app : ... originalExtras.putBoolean("foreground", false); originalExtras.putBoolean("coldstart", !isPushPluginActive); ... I haven't found how to receive these parameters at my source, eitherLiz
Hi rotoxl, I did find a way around this. I'll post my answer belowJulijulia
J
1

I eventually managed to solve this. It seems that during startup, in the following method the launchOptions parameter indicates if the app was started from a push notification or not.

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
...
}

In the method above i set the startup url for my application and provide an extra url parameter if the app was started from a push notification. So i now have the following scenario which solves my problem:

  • In the ondeviceready event read the startup parameter.
  • If the app was started from a push notification, do nothing and wait for the plugin to redirect to the right view
  • If the app was not started from a push notification redirect to the startup view.

Hope it helps someone.

Julijulia answered 1/3, 2015 at 22:40 Comment(0)
T
2

I guess you could workaround:

var coldstart = true;

// Update flag if app coldstart
document.addEventListener("pause", function() {
  coldstart = false;
}, false);
Toadeater answered 29/12, 2014 at 14:43 Comment(0)
J
1

I eventually managed to solve this. It seems that during startup, in the following method the launchOptions parameter indicates if the app was started from a push notification or not.

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
...
}

In the method above i set the startup url for my application and provide an extra url parameter if the app was started from a push notification. So i now have the following scenario which solves my problem:

  • In the ondeviceready event read the startup parameter.
  • If the app was started from a push notification, do nothing and wait for the plugin to redirect to the right view
  • If the app was not started from a push notification redirect to the startup view.

Hope it helps someone.

Julijulia answered 1/3, 2015 at 22:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.