XF 2.2 PHP - Detect if user browsing via PWA

Scandal

Well-known member
Is there any way to detect on the PHP side that the visitor is browsing via the progressive web app (PWA) ?
Do you have any suggestions to build this bool variable, without use of a cookie?
 
A _pwa=1 query param is added to the start URL, so I think you should be able to check for this and store the state in the session.
 
AFAIK this doesn't work 100% - I could start the App (which would flag the session), close the App and continue browsing in the Browser powering the App; it would still be the same session.
 
A _pwa=1 query param is added to the start URL, so I think you should be able to check for this and store the state in the session.
Hi Jeremy and thanks for your reply!
I did this:
On an extended Pub Controller Forum.php class on actionList method (home page):
PHP:
        \XF::runLater(function()
        {
            $is_pwa = \XF::app()->request()->filter('_pwa', 'uint');
            if ($is_pwa == 1)
            {
                $browser = \XF::app()->request()->getBrowser();
                if ($browser['os'] == 'android')
                {
                    $userId = intval(\XF::visitor()->user_id);
                    $ip = \XF::app()->request->getIp();
                    $binaryIp = \XF\Util\Ip::convertIpStringToBinary($ip);
                    $uniqueKey = ($userId ? $userId : $binaryIp);               
                    \XF::db()->query("UPDATE xf_session_activity SET sc_is_pwa = ? WHERE user_id = ? AND unique_key = ?", [1, $userId, $uniqueKey]);
                }
            }
        });
The runLater is useful in case someone makes a login to the forum when he is on forum home, which redirects back to /_?pwa=1 page (= the xf_session_activity table row has already replaced (with sc_is_pwa = 0), so later on the same script run, the system makes it again 1).

Scenario:
User opens the pwa page /?_pwa=1
The above code makes sc_is_pwa = 1 on xf_session_activity
The user starts browsing the forum (he leaves the /?_pwa=1 url).
The sc_is_pwa = 1 remains (ok).
Then user decides to make a login.
His xf_session_activity table row was replaced and now has the default sc_is_pwa = 0.
It is a bug since user had sc_is_pwa = 1.

Any idea how to fix?
 
FWIW I was speaking more to setting it in the session itself:
PHP:
\XF::session()->set('fromPwa', true)

Though as Kirby notes, this isn't entirely precise so YMMV.

To maintain the data across logins you'd probably want to hook \XF\ControllerPlugin\Login::completeLogin to check the value before and set it on the new session after.
 
Hey @Scandal , did you ever figure this out?

I would like to be able to have my who's online page show who is using pwa, so your sample code seemed just the ticket.
 
Back
Top Bottom