exeerik / apn
Requires
- php: ^7.2.5
- exeerik/pushok: ^0.11.0
- illuminate/config: ^7.0
- illuminate/events: ^7.0
- illuminate/notifications: ^7.0
- illuminate/support: ^7.0
Requires (Dev)
- mockery/mockery: ~1.3.0
- phpunit/phpunit: ~8.2
- squizlabs/php_codesniffer: ~3.5
README
此包使得使用Laravel通过Apple Push(APN)发送通知变得容易。
内容
安装
使用Composer安装此包
composer require laravel-notification-channels/apn
设置APN服务
在使用APN服务之前,请在您的应用中启用推送通知。然后,在“Certificates, Identifiers & Profiles”下创建一个APNS密钥以生成密钥ID和.p8文件。
收集您的密钥ID、团队ID(显示在苹果开发者页面的右上角)和应用程序包ID,并在config/broadcasting.php
中进行相应的配置。
'connections' => [ 'apn' => [ 'key_id' => env('APN_KEY_ID'), 'team_id' => env('APN_TEAM_ID'), 'app_bundle_id' => env('APN_BUNDLE_ID'), 'private_key_content' => env('APN_PRIVATE_KEY'), 'production' => env('APN_PRODUCTION', true), ], ],
有关客户端可以提供的参数的更多信息,请参阅pushok
文档 - 例如,您还可以使用private_key_path
和private_key_secret
。
使用
现在,您可以通过创建一个ApnMessage来向APN发送消息
use NotificationChannels\Apn\ApnChannel; use NotificationChannels\Apn\ApnMessage; use Illuminate\Notifications\Notification; class AccountApproved extends Notification { public function via($notifiable) { return [ApnChannel::class]; } public function toApn($notifiable) { return ApnMessage::create() ->badge(1) ->title('Account approved') ->body("Your {$notifiable->service} account was approved!"); } }
有关创建消息时可用方法的更多信息,请参阅ApnMessage
源代码。
在您的notifiable
模型中,确保包含一个routeNotificationForApn()
方法,该方法返回一个或多个令牌。
public function routeNotificationForApn() { return $this->apn_token; }
按消息配置
如果您需要为消息提供自定义配置,您可以提供一个Pushok客户端实例,它将代替默认客户端使用。
$customClient = new Pushok\Client(new Pushok\Token($options)); return ApnMessage::create() ->title('Account approved') ->body("Your {$notifiable->service} account was approved!") ->via($customClient)
VoIP推送通知
发送VoIP推送通知非常相似。您只需要使用ApnVoipChannel
通道和ApnVoipMessage
(它与常规的ApnMessage
具有相同的API)。
use NotificationChannels\Apn\ApnVoipChannel; use NotificationChannels\Apn\ApnVoipMessage; use Illuminate\Notifications\Notification; class AccountApproved extends Notification { public function via($notifiable) { return [ApnVoipChannel::class]; } public function toApnVoip($notifiable) { return ApnVoipMessage::create() ->badge(1); } }
在您的notifiable
模型中,确保包含一个routeNotificationForApnVoip()
方法,该方法返回一个或多个令牌。
public function routeNotificationForApnVoip() { return $this->apn_voip_token; }
变更日志
有关最近更改的更多信息,请参阅变更日志。
测试
$ composer test
安全性
如果您发现任何安全相关的问题,请通过电子邮件info@fruitcake.nl联系,而不是使用问题跟踪器。
贡献
有关详细信息的更多信息,请参阅贡献。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。