Skip to content
APN push notifications channel for Laravel
PHP
Branch: master
Clone or download
Latest commit 338b8a1 Oct 4, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src StyleCI fixes Oct 4, 2019
tests Add support for Laravel 6.0 Sep 5, 2019
.editorconfig Initial commit Aug 17, 2016
.gitattributes Initial commit Aug 17, 2016
.gitignore Initial commit Aug 17, 2016
.scrutinizer.yml Initial commit Aug 17, 2016
.styleci.yml Fix StyleCI Oct 4, 2019
.travis.yml Adjust test versions Sep 5, 2019
CHANGELOG.md Updates (#24) Feb 4, 2017
CONTRIBUTING.md Updates (#24) Feb 4, 2017
LICENSE.md Initial commit Aug 17, 2016
README.md StyleCI fixes Oct 4, 2019
composer.json Adjust version constraint Sep 5, 2019
phpunit.xml.dist Add support for Laravel 6.0 Sep 5, 2019

README.md

Laravel APN (Apple Push) Notification Channel

Latest Version on Packagist Software License Build Status StyleCI SensioLabsInsight Quality Score Code Coverage Total Downloads

This package makes it easy to send notifications using Apple Push (APN) with Laravel.

Contents

Installation

Install this package with Composer:

composer require laravel-notification-channels/apn

If you're using Laravel 5.x you'll also need to specify a version constraint:

composer require laravel-notification-channels/apn -v 0.4.0

Setting up the APN service

Before using the APN Service, enable Push Notifications in your app. Then, generate a p12 certificate and convert it into a .pem file so that we can use it with this library:

  • Run this command to convert your p12 file to a pem file:

    $ openssl pkcs12 -nodes -in Push.p12 -out Push.pem

  • Make sure your server or development machine can send outgoing traffic on port 2195 and on port 2196 (for the feedback service, see below)

  • Run this command to make sure your certificates are valid:

    $ openssl s_client -cert Push.pem -connect gateway.push.apple.com:2195

    (use gateway.sandbox.push.apple.com if you have a dev push certificate)

  • If the certificates are valid, the server should not hang up

You need to successfully complete the above before you can use this channel. Once you have done so, you can configure the path to the Push.pem certificate in config/broadcasting.php

    'connections' => [

      'apn' => [
          'environment' => ApnChannel::PRODUCTION, // Or ApnChannel::SANDBOX
          'certificate' => '/path/to/certificate.pem', 
          'pass_phrase' => null, // Optional passPhrase
      ],

    ]

Usage

You can now send messages to APN by creating a ApnMessage:

use NotificationChannels\Apn\ApnChannel;
use NotificationChannels\Apn\ApnMessage;
use Illuminate\Notifications\Notification;

class AccountApproved extends Notification
{
    public function via($notifiable)
    {
        return [ApnChannel::class]; // Make sure you use 'ApnChannel::class', not just 'apn'
    }

    public function toApn($notifiable)
    {
        return ApnMessage::create()
            ->badge(1)
            ->title('Account approved')
            ->body("Your {$notifiable->service} account was approved!");
    }
}

In your notifiable model, make sure to include a routeNotificationForApn() method, which return one or an array of tokens.

public function routeNotificationForApn()
{
    return $this->apn_token;
}

Available methods

  • title($str)
  • body($str)
  • badge($integer)
  • custom($customData)

Feedback Service

Apple implements a Feedback Service. See the Zend APN documentation

APNS has a feedback service that you must listen to. Apple states that they monitor providers to ensure that they are listening to this service.

The feedback service simply returns an array of Feedback responses. All tokens provided in the feedback should not be sent to again; unless the device re-registers for push notification. You can use the time in the Feedback response to ensure that the device has not re-registered for push notifications since the last send.

One way to use the Feedback Service is by using Laravel's task scheduling functionality. All tokens returned from the feedback service should be removed from the system and should not be sent again.

use App\User;
use NotificationChannels\Apn\FeedbackService;
use NotificationChannels\Apn\ApnFeedback;

$feedbackService = app(FeedbackService::class);

/** @var ApnFeedback $feedback */
foreach ($feedbackService->get() as $feedback) {
    User::where('apn_token', $feedback->token)
        ->update(['apn_token' => null]);
}

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email info@fruitcake.nl instead of using the issue tracker.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

You can’t perform that action at this time.