PHPackages                             ricwein/push-notifications - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. ricwein/push-notifications

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

ricwein/push-notifications
==========================

Push Notifications for iOS (APNS), Android (FCM)

3.0.1(2y ago)2724.8k↓38.9%5[1 issues](https://github.com/ricwein/PushNotifications/issues)MITPHPPHP ^8.0

Since Feb 28Pushed 2y ago3 watchersCompare

[ Source](https://github.com/ricwein/PushNotifications)[ Packagist](https://packagist.org/packages/ricwein/push-notifications)[ RSS](/packages/ricwein-push-notifications/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (20)Used By (0)

PushNotification
================

[](#pushnotification)

... is a small php-library to wrap Apple (APNS) and Google (FCM) Push-Notifications into a simple syntax.

Examples:

### Android

[](#android)

```
use ricwein\PushNotification\{PushNotification, Message, Handler};

$message = new Message('message', 'title', ['payload' => 'data']);
$fcm = new Handler\FCM('ExampleGooglePushToken12345678987654321');

$push = new PushNotification(['fcm' => $fcm]);
$push->send($message, ['' => 'fcm']);
```

### iOS

[](#ios)

> NOTE: The `APNS` Handler uses the *new* apple push servers, which require HTTP2. Therefore, curl with HTTP2 support must be installed.

```
use ricwein\PushNotification\{PushNotification, Message, Handler, Config};
use Pushok\AuthProvider;

$message = new Message('message', 'title', ['payload' => 'data']);
$apns = new Handler\APNS(AuthProvider\Token::create([
    'key_id' => 'AAAABBBBCC', // The Key ID obtained from Apple developer account
    'team_id' => 'DDDDEEEEFF', // The Team ID obtained from Apple developer account
    'app_bundle_id' => 'com.app.Test', // The bundle ID for app obtained from Apple developer account
    'private_key_path' => __DIR__ . '/private_key.p8', // Path to private key
    'private_key_secret' => null // Private key secret
]), Config::ENV_PRODUCTION);

$push = new PushNotification(['apns' => $apns]);
$push->send($message, ['' => 'apns']);
```

### mixed

[](#mixed)

Sending messages to multiple devices of difference operating systems is also simple:

```
use ricwein\PushNotification\{PushNotification, Message, Handler, Config};

$message = new Message('message', 'title');
$fcm = new Handler\FCM('ExampleGooglePushToken12345678987654321');
$apns = new Handler\APNS($authToken, Config::ENV_PRODUCTION);

$push = new PushNotification(['apns' => $apns, 'fcm' => $fcm]);
$push->send($message, [
    '' => 'apns',
    '' => 'apns',
    '' => 'fcm',
    '' => 'fcm',
]);
```

### single message / single handler

[](#single-message--single-handler)

For single handler messages it's possible to inline the handler into the device-destination array. The handler is then freed automatically after the message was send.

```
use ricwein\PushNotification\{PushNotification, Message, Handler};

$result = (new PushNotification)->send(new Message($body,$title), [
    '' => new Handler\(...$config);
]);
```

usage
-----

[](#usage)

This class uses the root-namespace `ricwein\PushNotification`.

### init

[](#init)

The libraries main class is called `PushNotification` and requires an array of available push-handlers for the constructor. It's possible to set an ID as the handlers array key, to allow assigning devices to the handler later on.

Available push-handler are:

- Apple: `PushNotification\Handler\APNS`
- Google: `PushNotification\Handler\FCM`

They're all extending `PushNotification\Handler`

### configuration

[](#configuration)

Since all push-settings are push-handler specific, the settings are directly applied in the handler constructors.

- APNS:

```
 new APNS(
    \Pushok\AuthProviderInterface $authProvider, /* @see https://github.com/edamov/pushok/blob/master/README.md#getting-started */
    string $environment /* (Config::ENV_PRODUCTION / Config::ENV_DEVELOPMENT / Config::ENV_CUSTOM) */
)
```

- FCM:

```
 new FCM(
    string $token,
    ?string $caCertPath = null,
    string $url = self::FCM_ENDPOINT,
    int $timeout = 10
)
```

It's also possible to have multiple push-handlers with different configurations like:

```
use ricwein\PushNotification\{PushNotification, Message, Handler, Config};

// @see https://github.com/edamov/pushok
$apnsProd = new Handler\APNS($tokenProd, Config::ENV_PRODUCTION);
$apnsDev = new Handler\APNS($tokenDev, Config::ENV_DEVELOPMENT);

$message = new Message('message', 'title');
$push = new PushNotification(['prod' => $apnsProd, 'dev' => $apnsDev]);

$push->send($message, [
    '' => 'prod',
    '' => 'dev',
]);
```

### sending

[](#sending)

Sending is either available for a message object or a raw payload.

- A message object is translated into a native push-notification message with body and title for FCM or APNS before sending.
- A raw payload (array) is sent '*as it is*' which might **not** be a good idea, if you want to mix APNS and FCM in one request.

```
use ricwein\PushNotification\{Message, Config};

$devices = [...];

$message = new Message('body', 'title');
$message->setSound('test.aiff')->setBadge(2)->setPriority(Config::PRIORITY_NORMAL);
$push->send($message, $devices);

/* OR */

$payload = [...];
$push->sendRaw($payload, $devices);
```

### error handling

[](#error-handling)

The `PushNotification::send()` method returns an `Result` object. This usually contains an array of per device errors. If everything succeeded, the entry is null. You can fetch failed device-messages with:

```
$result = $push->send($message, [...]);
$errors = $result->getFailed();
```

Errors are handled as Exceptions, so it's possible to just throw them. To simply just throw the first error if one occurred, call:

```
$push->send($message, [...])->throwOnFirstException();
```

***Be aware***: Sometimes other failures than usage-errors occur. APNS and FCM can respond with explicit reasons, which will be handled as `ResponseReasonException`. **It's a good idea to not just throw them (away)**, but handle them other ways. E.g. you might want to delete or update device-tokens which were marked as invalid.

```
use \ricwein\PushNotification\Exceptions\ResponseReasonException;

foreach($result->getFailed() as $token => $error) {
    if ($error instanceof ResponseReasonException) {
        if ($error->isInvalidDeviceToken()) {
            // $token was invalid
        } elseif ($error->isRateLimited()) {
            // the $token device got too many notifications and is currently rate-limited => better wait some time before sending again.
        }

    }
}
```

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity38

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 96.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~115 days

Recently: every ~284 days

Total

19

Last Release

919d ago

Major Versions

1.1.1 → 2.02020-09-15

2.4.7 → 3.02021-05-03

PHP version history (5 changes)1.0PHP &gt;= 7.0.0

1.1.0PHP &gt;= 7.2.0

2.3PHP &gt;= 7.4.0

3.0PHP &gt;= 8.0

3.0.1PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/1e4c46b9e849423ff1355ff0a8dc5046c29e636308378f44ed0a55933f749e83?d=identicon)[ricwein](/maintainers/ricwein)

---

Top Contributors

[![ricwein](https://avatars.githubusercontent.com/u/870354?v=4)](https://github.com/ricwein "ricwein (57 commits)")[![mackrais](https://avatars.githubusercontent.com/u/8103057?v=4)](https://github.com/mackrais "mackrais (2 commits)")

---

Tags

androidapnsapplefcmgcmphppush-notificationspushnotification

### Embed Badge

![Health badge](/badges/ricwein-push-notifications/health.svg)

```
[![Health](https://phpackages.com/badges/ricwein-push-notifications/health.svg)](https://phpackages.com/packages/ricwein-push-notifications)
```

###  Alternatives

[minishlink/web-push

Web Push library for PHP

1.9k12.0M53](/packages/minishlink-web-push)[laravel-notification-channels/apn

Apple APN Push Notification Channel

2021.9M4](/packages/laravel-notification-channels-apn)[laravel-notification-channels/twilio

Provides Twilio notification channel for Laravel

2587.7M12](/packages/laravel-notification-channels-twilio)[spatie/url-signer

Generate a url with an expiration date and signature to prevent unauthorized access

4422.3M16](/packages/spatie-url-signer)[mattketmo/email-checker

Throwaway email detection library

2742.0M5](/packages/mattketmo-email-checker)[netflie/laravel-notification-whatsapp

Laravel notification driver for WhatsApp

176173.9k](/packages/netflie-laravel-notification-whatsapp)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
