Phonegap-plugin-push and cordova-plugin-background-mode problem on ios

Hello noobs! :smiley:

I know that this forum is almost dead for middle+ level difficulty questions but I will try one more time.

I am almost sure that I will not receive an answer that will help, but maybe my info will help someone in future.

So, when you receive notification from phonegap-plugin-push, your app will do all starting logic without show app view. In my case I need to play some music.

But ios specifications does not allow to play music, when app is initiated from push notification. This problem can be solved with cordova-plugin-local-notifications. This plugin allow to play sound even from push notification callback during 8 seconds (ios docs allow to play up to 30, but this plugin allow only 8 second during notification shows on display). This is not good behaviour, but it works and not urgent.

The really urgent problem is that I need different logic for different app states. Actually app has two states - background and foreground, but really app has three states:

  • app is initiated from push notification - show local notification + play sound
  • app is launched by user and it is in foreground - not show local notification, just play sound
  • app is launched by user and it is in background - show local notification + play sound

To detect app state I tried following methods, and no one works as expected:

  • cordova-plugin-background-mode - this plugin has method isActive(), but it not work if app is initiated from push notification
  • use coldstart field from push notification - this property not work at all
  • use document.hasFocus() - this method works but has side effect. It will always return false, even if user is opened app but did not touch device screen When user open app and touch screen at least one time, method will always return true, even if app is moved to background
  • use cordova events (code below) - not worked due to
    – when app is initiated from push notification, or this is first app launch after full exit - both property will be undefined (events not fired for this situatuins)
public constructor(
        private _platform: Platform
    ) {}

private _initBackgroundEvents(): void {
        this._platform.pause.subscribe(() => {
            this._isAppInForegroundFromEvent = false;
            this._isAppInBackgroundFromEvent = true;
        });

        this._platform.resume.subscribe(() => {
            this._isAppInForegroundFromEvent = true;
            this._isAppInBackgroundFromEvent = false;
        });
    }

So is there any chance to detect app states correctly?

I wish I could be more useful but it looks like you already did your maths. I would just have one input, where did you call ‘_initBackgroundEvents()’? In ngoninit or in constructor? Somehow somewhere I remember that added it in constructor helped me at some point