Minishlink\WebPush\WebPush::sendNotification PHP Method

sendNotification() public method

Send a notification.
public sendNotification ( string $endpoint, string | null $payload = null, string | null $userPublicKey = null, string | null $userAuthToken = null, boolean $flush = false, array $options = [], array $auth = [] ) : array | boolean
$endpoint string
$payload string | null If you want to send an array, json_encode it
$userPublicKey string | null
$userAuthToken string | null
$flush boolean If you want to flush directly (usually when you send only one notification)
$options array Array with several options tied to this notification. If not set, will use the default options that you can set in the WebPush object
$auth array Use this auth details instead of what you provided when creating WebPush
return array | boolean Return an array of information if $flush is set to true and the queued requests has failed. Else return true
    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;
    }

Usage Example

Beispiel #1
0
 public function testSendGCMNotificationWithWrongGCMApiKey()
 {
     $webPush = new WebPush(array('GCM' => 'bar'));
     $res = $webPush->sendNotification($this->endpoints['GCM'], null, null, true);
     $this->assertTrue(is_array($res));
     // there has been an error
     $this->assertArrayHasKey('success', $res);
     $this->assertEquals(false, $res['success']);
     $this->assertArrayHasKey('statusCode', $res);
     $this->assertEquals(401, $res['statusCode']);
     $this->assertArrayHasKey('headers', $res);
 }
All Usage Examples Of Minishlink\WebPush\WebPush::sendNotification