exeerik/apn

苹果APN推送通知通道

v2.2.1 2020-04-02 16:31 UTC

README

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

此包使得使用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_pathprivate_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)。有关更多信息,请参阅许可证文件