Cordova detects cold start from Push Notification

I have a cordova (codova (3.4.0) app running on iOS and soon also on Android. Push notifications are implemented and working. I'm having problems when the app is launched via push notification and redirects the app to the right page.

Note. This does not apply to launching an app from a push notification while the app is running in the background. Only when the application is completely closed!

I now have the following workflow:

Normal startup:

  • Wait until cordova.js fires the ondeviceready event.
  • On the ondeviceready event redirects to the initial view of my app (via window.location.hash)

Cold launching an app from a push notification:

  • Wait until cordova.js fires the ondeviceready event.
  • On the ondeviceready event redirects to the initial view of my app (via window.location.hash)
  • Plugin launches and runs a javascript function in my app named notificationreceived
  • In notifiable function, function redirects view based on push notification

As you can see, the cold application startup script goes into the startup view, which is unnecessary and forces the user to wait for the first view to load, but only for redirection.

The problem is that when deviceready fires, my javascript code doesn't yet know about the upcoming push notification, so I'm looking for a way to work around that.

Is there a way, perhaps, to pass additional parameters to the cordiceready event cordova? Or does anyone have any other idea or solution to solve this problem?

+3


source to share


2 answers


I think you could workaround:



var coldstart = true;

// Update flag if app coldstart
document.addEventListener("pause", function() {
  coldstart = false;
}, false);

      

+2


source


Ultimately I managed to solve this. It looks like when launched in the following method, the launchOptions parameter indicates whether the app was launched from a push notification or not.

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

      

In the above method, I am setting the launch url for my app and provide an additional url parameter if the app was launched from a push notification. Therefore, I now have the following script that solves my problem:



  • In the ondeviceready event, read the launch parameter.
  • If the app was launched from a push notification, do nothing and wait for the plugin to redirect it to the correct view
  • If the app didn't start from the push notification redirected to the initial view.

Hope this helps someone.

0


source







All Articles