Ph3nol / NotificationPusher

Standalone PHP library for easy devices notifications push.
MIT License
1.18k stars 274 forks source link

Zend dependencies are abandoned #197

Open risototh opened 4 years ago

risototh commented 4 years ago

Composer outputs:

Package zendframework/zend-stdlib is abandoned, you should avoid using it. Use laminas/laminas-stdlib instead. Package zendframework/zend-validator is abandoned, you should avoid using it. Use laminas/laminas-validator instead. Package zendframework/zend-json is abandoned, you should avoid using it. Use laminas/laminas-json instead. Package zendframework/zend-escaper is abandoned, you should avoid using it. Use laminas/laminas-escaper instead. Package zendframework/zend-uri is abandoned, you should avoid using it. Use laminas/laminas-uri instead. Package zendframework/zend-loader is abandoned, you should avoid using it. Use laminas/laminas-loader instead. Package zendframework/zend-http is abandoned, you should avoid using it. Use laminas/laminas-http instead. Package zendframework/zendservice-google-gcm is abandoned, you should avoid using it. No replacement was suggested. Package zendframework/zendservice-apple-apns is abandoned, you should avoid using it. No replacement was suggested.

seyfer commented 4 years ago

@risototh yes this is known. Do you have time to contribute?

risototh commented 4 years ago

@seyfer not sure, if I have the time. Depends, on how much it will require. I only went around, when updating this library from the 2.0.0 version in our code ;D Maybe I can spent some hours on that, but I don't even know, what this library is for and how to test it...

kw-pr commented 3 years ago

We are running out of time here. Apple just wrote:

If you still send push notifications with the legacy binary protocol, make sure to upgrade to the APNs provider API as soon as possible. APNs will no longer support the legacy binary protocol after March 31, 2021.

sarveshamrit commented 3 years ago

@risototh Sir any update , i am not able to send push to apple anymore, it goes to android but not to ios , on same side it goes when i try from pushtry.com? @seyfer @kw-pr

lstrojny commented 3 years ago

To get the ball rolling, here is a quick implementation of an HTTP based adapter. It uses apple/apn-push. It’s probably not complete, probably has bugs and so on but it works for simple cases in my testing.

class ApnsHttp extends BaseAdapter
{
    public function push(PushInterface $push): DeviceCollection
    {
        $certificate = new Certificate($this->getParameter('certificate'), $this->getParameter('passPhrase'));
        $authenticator = new CertificateAuthenticator($certificate);

        $builder = new Http20Builder($authenticator);
        $sender = $builder->build();
        $pushedDevices = new DeviceCollection();

        /** @var DeviceInterface $device */
        foreach ($push->getDevices() as $device) {
            $receiver = new Receiver(new DeviceToken($device->getToken()), $this->getParameter('bundleId'));
            try {
                $sender->send($receiver, $this->toNotification($device, $push->getMessage()), $this->isDevelopmentEnvironment());
                $push->addResponse($device, ['success' => true]);
                $push->pushed();

                $pushedDevices->add($device);
            } catch (SendNotificationException $e) {
                throw new PushException($e->getMessage(), null, $e);
            }
        }

        return $pushedDevices;
    }

    private function toNotification(DeviceInterface $device, Message $message): Notification
    {
        $alert = (new Alert())
            ->withBody($message->getText())
            ->withTitle($message->getOption('title') ?? '')
            ->withLaunchImage($message->getOption('launchImage') ?? '');

        if ($message->getOption('locKey')) {
            $alert = $alert->withBodyLocalized(
                new Localized($message->getOption('locKey'), $message->getOption('locArgs', []))
            );
        }

        if ($message->getOption('actionLocKey')) {
            $alert = $alert->withActionLocalized(
                new Localized($message->getOption('actionLocKey'), $message->getOption('actionLocKeyArgs', []))
            );
        }

        if ($message->getOption('titleLocKey')) {
            $alert = $alert->withLocalizedTitle(
                new Localized($message->getOption('titleLocKey'), $message->getOption('titleLocArgs', []))
            );
        }

        $aps = (new Aps($alert))
            ->withSound($message->getOption('sound') ?? '')
            ->withContentAvailable($message->getOption('content-available', false))
            ->withMutableContent($message->getOption('mutable-content', false))
            ->withCategory($message->getOption('category') ?? '');

        if ($message->hasOption('badge')) {
            $aps = $aps->withBadge($message->getOption('badge') + $device->getParameter('badge', 0));
        }

        $payload = (new Payload($aps));

        foreach ($message->getOption('custom', []) as $key => $value) {
            $payload = $payload->withCustomData($key, $value);
        }

        return (new Notification($payload))
            ->withExpiration(
                $message->getOption('expire') ? new Expiration(new DateTime($message->getOption('expire'))) : null
            );
    }

    public function supports($token): bool
    {
        return ctype_xdigit($token);
    }

    public function getDefinedParameters(): array
    {
        return [];
    }

    public function getDefaultParameters(): array
    {
        return ['passPhrase' => ''];
    }

    public function getRequiredParameters(): array
    {
        return ['certificate', 'bundleId'];
    }
}