githubEdit

The Notification

To reach the client (web browser), you need to send a Notification to the Subscription.

<?php
use WebPush\Notification;

$notification = Notification::create();

The Notification should have a payload. In this case, the payload will be encrypted on server side and decrypted by the client.

That payload may be a string or a JSON object. The structure of the latter is described in the next section.

<?php
use WebPush\Notification;

$notification = Notification::create()
    ->withPayload('Hello world')
;

TTL (Time-To-Live)

With this feature, a value in seconds is added to the notification. It suggests how long a push message is retained by the push service. A value of 0 (zero) indicates the notification is delivered immediately.

Using TTL Constants

The library provides predefined constants for common TTL values:

Using Custom TTL Values

You can also specify custom TTL values in seconds:

Topic

A push message that has been stored by the push service can be replaced with new content. If the user agent is offline during the time the push messages are sent, updating a push message avoids the situation where outdated or redundant messages are sent to the user agent.

Only push messages that have been assigned a topic can be replaced. A push message with a topic replaces any outstanding push message with an identical topic.

Urgency

For a device that is battery-powered, it is often critical it remains dormant for extended periods.

Radio communication in particular consumes significant power and limits the length of time the device can operate.

To avoid consuming resources to receive trivial messages, it is helpful if an application server can communicate the urgency of a message and if the user agent can request that the push server only forwards messages of a specific urgency.

Urgency
Device State
Examples

very-low

On power and Wi-Fi

Advertisements

low

On either power or Wi-Fi

Topic updates

normal

On neither power nor Wi-Fi

Chat or Calendar Message

high

Low battery

Incoming phone call or time-sensitive alert

circle-exclamation

Asynchronous Response

Your application may prefer asynchronous responses to request confirmation from the push service when a push message is delivered and then acknowledged by the user agent. The push service MUST support delivery confirmations to use this feature.

circle-exclamation

JSON Messages

As mentioned in the overview section, the specification defines a structure for the payloadarrow-up-right. This structure contains properties that the client should understand and render in an appropriate way.

The library provides a WebPush\Message class with convenient methods to ease the creation of a message.

The resulting notification payload will look as follows:

On client side, you can easily load that payload and display the notification:

Best Practices

Choose Appropriate TTL Values

The Time-To-Live (TTL) determines how long notifications are retained if the user is offline:

Use Topics Wisely

Topics prevent notification spam by replacing old notifications with new ones:

Set Appropriate Urgency

Match urgency to content to optimize battery life:

Craft Effective Messages

Good notification messages are:

  • Clear: User understands the content immediately

  • Actionable: User knows what to do next

  • Concise: Get to the point quickly

  • Relevant: Personalized to the user

Optimize for Mobile

Mobile devices have limited battery and screen space:

Common Notification Patterns

1. Chat Message

2. System Alert

3. News Update

4. Silent Background Sync

Handling Delivery Failures

Always handle failures gracefully:

Testing Notifications

Always test your notifications before sending to production:

Performance Considerations

Batch Sending

When sending to many subscriptions, batch efficiently:

Respect Rate Limits

Push services have rate limits. Implement throttling:

Validation Exceptions

The library throws specific exceptions when notification properties are invalid. Each exception provides direct access to the problematic value for better debugging.

Available Exceptions

InvalidTopicException

Thrown when topic validation fails. Access the invalid topic via $e->topic:

Common causes:

  • Topic exceeds 32 characters

  • Topic contains invalid characters (only a-z, A-Z, 0-9, -, ., _, ~ allowed)

  • Empty topic

InvalidTTLException

Thrown when TTL is negative. Access the invalid TTL via $e->ttl:

InvalidUrgencyException

Thrown when urgency is not a valid value. Access the invalid urgency via $e->urgency:

Valid urgency values: very-low, low, normal, high

Handling Multiple Validations

Catch specific exceptions for targeted error handling:

circle-info

See the Exceptions page for complete documentation on error handling strategies and best practices.

Next Steps

Last updated

Was this helpful?