/** * Send a notification. * * @param string $endpoint * @param string|null $payload If you want to send an array, json_encode it * @param string|null $userPublicKey * @param string|null $userAuthToken * @param bool $flush If you want to flush directly (usually when you send only one notification) * @param array $options Array with several options tied to this notification. If not set, will use the default options that you can set in the WebPush object * @param array $auth Use this auth details instead of what you provided when creating WebPush * * @return array|bool Return an array of information if $flush is set to true and the queued requests has failed. * Else return true * * @throws \ErrorException */ public function sendNotification($endpoint, $payload = null, $userPublicKey = null, $userAuthToken = null, $flush = false, $options = array(), $auth = array()) { if (isset($payload)) { if (Utils::safeStrlen($payload) > Encryption::MAX_PAYLOAD_LENGTH) { throw new \ErrorException('Size of payload must not be greater than ' . Encryption::MAX_PAYLOAD_LENGTH . ' octets.'); } $payload = Encryption::padPayload($payload, $this->automaticPadding); } if (array_key_exists('VAPID', $auth)) { $auth['VAPID'] = VAPID::validate($auth['VAPID']); } $this->notifications[] = new Notification($endpoint, $payload, $userPublicKey, $userAuthToken, $options, $auth); if ($flush) { $res = $this->flush(); // if there has been a problem with at least one notification if (is_array($res)) { // if there was only one notification, return the information directly if (count($res) === 1) { return $res[0]; } return $res; } return true; } return true; }
/** * @dataProvider vapidProvider * * @param $audience * @param $vapid * @param $expiration * @param $expectedAuthorization * @param $expectedCryptoKey */ public function testGetVapidHeaders($audience, $vapid, $expiration, $expectedAuthorization, $expectedCryptoKey) { $vapid = VAPID::validate($vapid); $headers = VAPID::getVapidHeaders($audience, $vapid['subject'], $vapid['publicKey'], $vapid['privateKey'], $expiration); $this->assertArrayHasKey('Authorization', $headers); $this->assertEquals(Utils::safeStrlen($expectedAuthorization), Utils::safeStrlen($headers['Authorization'])); $this->assertEquals($this->explodeAuthorization($expectedAuthorization), $this->explodeAuthorization($headers['Authorization'])); $this->assertArrayHasKey('Crypto-Key', $headers); $this->assertEquals($expectedCryptoKey, $headers['Crypto-Key']); }