src/Model/PushNotificationModel.php
public function saveSubscription(int $userId = null) : void
    {
        $json = \file_get_contents('php://input');

        if (!$json)
        {
            return;
        }

        $data = \json_decode($json, true);

        if (!$data || empty($data['endpoint']) || empty($data['publicKey']) || empty($data['authToken']))
        {
            return;
        }

        $row = $this->subscriptionTable->findActive()->where('endpoint', $data['endpoint'])->fetch();

        switch ($this->request->getMethod()) {
            case 'POST':
            case 'PUT':
            {
                if ($row)
                {
                    $row->update([
                        'user_id' => $userId ?: $row->user_id ?: null,
                        'key' => $data['publicKey'],
                        'token' => $data['authToken'],
                        'encoding' => $data['contentEncoding']
                    ]);
                    
                    return;
                }

                $row = $this->subscriptionTable->insert([
                    'user_id' => $userId,
                    'endpoint' => $data['endpoint'],
                    'key' => $data['publicKey'],
                    'token' => $data['authToken'],
                    'encoding' => $data['contentEncoding']
                ]);

                $this->sendNotification($row, 'Notifications enabled!', true);

                return;
            }
            case 'DELETE':
            {
                if ($row)
                {
                    $row->update([
                        'active' => 0
                    ]);
                }

                return;
            }
            default:
                throw new \Nette\Application\BadRequestException();
        }
    }
public function saveSubscription(int $userId = null) : void
    {
        $json = \file_get_contents('php://input');

        if (!$json)
        {
            return;
        }

        $data = \json_decode($json, true);

        if (!$data || empty($data['endpoint']) || empty($data['publicKey']) || empty($data['authToken']))
        {
            return;
        }

        $row = $this->subscriptionTable->findActive()->where('endpoint', $data['endpoint'])->fetch();

        switch ($this->request->getMethod()) {
            case 'POST':
            case 'PUT':
            {
                if ($row)
                {
                    $row->update([
                        'user_id' => $userId ?: $row->user_id ?: null,
                        'key' => $data['publicKey'],
                        'token' => $data['authToken'],
                        'encoding' => $data['contentEncoding']
                    ]);
                    
                    return;
                }

                $row = $this->subscriptionTable->insert([
                    'user_id' => $userId,
                    'endpoint' => $data['endpoint'],
                    'key' => $data['publicKey'],
                    'token' => $data['authToken'],
                    'encoding' => $data['contentEncoding']
                ]);

                $this->sendNotification($row, 'Notifications enabled!', true);

                return;
            }
            case 'DELETE':
            {
                if ($row)
                {
                    $row->update([
                        'active' => 0
                    ]);
                }

                return;
            }
            default:
                throw new \Nette\Application\BadRequestException();
        }
    }
public function saveSubscription ( int $ userId = null ) : void { $ json = \ file_get_contents ( 'php://input' ) ; if ( ! $ json ) { return ; } $ data = \ json_decode ( $ json , true ) ; if ( ! $ data || empty ( $ data [ 'endpoint' ] ) || empty ( $ data [ 'publicKey' ] ) || empty ( $ data [ 'authToken' ] ) ) { return ; } $ row = $ this -> subscriptionTable -> findActive ( ) -> where ( 'endpoint' , $ data [ 'endpoint' ] ) -> fetch ( ) ; switch ( $ this -> request -> getMethod ( ) ) { case 'POST' : case 'PUT' : { if ( $ row ) { $ row -> update ( [ 'user_id' => $ userId ? : $ row -> user_id ? : null , 'key' => $ data [ 'publicKey' ] , 'token' => $ data [ 'authToken' ] , 'encoding' => $ data [ 'contentEncoding' ] ] ) ; return ; } $ row = $ this -> subscriptionTable -> insert ( [ 'user_id' => $ userId , 'endpoint' => $ data [ 'endpoint' ] , 'key' => $ data [ 'publicKey' ] , 'token' => $ data [ 'authToken' ] , 'encoding' => $ data [ 'contentEncoding' ] ] ) ; $ this -> sendNotification ( $ row , 'Notifications enabled!' , true ) ; return ; } case 'DELETE' : { if ( $ row ) { $ row -> update ( [ 'active' => 0 ] ) ; } return ; } default : throw new \ Nette \ Application \ BadRequestException ( ) ; } }
Save or update subscriber information.
@throws \Nette\Application\BadRequestException
@param int $userId
Save or update subscriber information .