brdevelopers/laravel-apn-notification-channel

Laravel 6 的 Apple Push Notification Service (APNs) 通知通道,使用基于令牌的新 APNs HTTP/2 协议(JWT 与 p8 私钥)

2.0.0 2023-08-10 09:37 UTC

This package is not auto-updated.

Last update: 2024-10-03 15:09:00 UTC


README

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

本软件包使您能够轻松使用 Laravel 6 通过新 APNs HTTP/2 协议(基于令牌,JWT 与 p8 私钥)向 iOS 发送通知。

内容

功能

  • 使用新的 Apple APNs HTTP/2 连接
  • 支持基于 JWT 的身份验证
  • 支持基于证书的身份验证
  • 支持新的 iOS 10 功能,如折叠 ID、副标题和可变通知
  • 使用并发请求到 APNs
  • 已在 APNs 生产环境中测试和运行

要求

  • PHP >= 7.2
  • lib-curl >= 7.46.0(启用 http/2 支持)
  • lib-openssl >= 1.0.2e

安装

使用 Composer 安装此软件包

composer require brdevelopers/laravel-apn-notification-channel

如果您在 Laravel 5.4 或更低版本中安装此软件包,则必须导入服务提供者

// config/app.php
'providers' => [
    // ...
    BRDevelopers\ApnNotificationChannel\ApnServiceProvider::class,
],

设置 APN 服务

将凭证添加到您的 config/broadcasting.php

如果您正在使用基于 JWT 的身份验证

// config/broadcasting.php
'connections' => [
    ...
    'apn' => [
        'driver' => 'jwt',
        'is_production' => env('APP_ENV') === 'production',
        'key_id' => env('APN_KEY_ID'), // The Key ID of the p8 file (available at https://developer.apple.com/account/ios/authkey/)
        'team_id' => env('APN_TEAM_ID'), // The Team ID of your Apple Developer Account (available at https://developer.apple.com/account/#/membership/)
        'app_bundle_id' => env('APN_APP_BUNDLE_ID'), // The Bundle ID of your application. For example, "com.company.application"
        'private_key_path' => env('APN_PRIVATE_KEY', storage_path('apns-private-key.p8')),
        'private_key_secret' => env('APN_PRIVATE_KEY_SECRET'),
    ],
    ...
],

如果您正在使用基于证书的身份验证

// config/broadcasting.php
'connections' => [
    ...
    'apn' => [
        'driver' => 'certificate',
        'is_production' => env('APP_ENV') === 'production',
        'certificate_path' => env('APN_CERTIFICATE_PATH', storage_path('apns-certificate.pem')),
        'certificate_secret' => env('APN_CERTIFICATE_SECRET'),
    ],
    ...
],

用法

现在您可以在通知中的 via() 方法中使用此通道

use Illuminate\Notifications\Notification;
use BRDevelopers\ApnNotificationChannel\ApnMessage;

class AccountApproved extends Notification
{
    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['apn'];
    }

    /**
     * Get the APN representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return ApnMessage
     */
    public function toApn($notifiable)
    {
        return ApnMessage::create()
            ->badge(1)
            ->title('Account approved')
            ->body("Your {$notifiable->service} account was approved!");
    }
}

在您的 notifiable 模型中,确保包含一个返回一个或多个设备令牌的 routeNotificationForApn() 方法。

/**
 * Route notifications for the APN channel.
 *
 * @return string|array
 */
public function routeNotificationForApn()
{
    return $this->apn_token;
}

可用的消息方法

  • title($str)
  • subtitle($str)
  • body($str)
  • badge($int)
  • sound($str)
  • category($str)
  • custom($key, $value)
  • setCustom($array)
  • titleLocKey($str)
  • titleLocArgs($array)
  • actionLocKey($str)
  • setLocKey($str)
  • setLocArgs($array)
  • launchImage($str)
  • contentAvailability($bool)
  • mutableContent($bool)
  • threadId($str)

待办事项

  • 修复 Travis CI
  • 修复 Scrutinizer CI 代码覆盖率
  • 添加测试

更新日志

请参阅 更新日志 了解最近的变化。

测试

$ composer test

安全

如果您发现任何安全相关的问题,请通过电子邮件 semyon.chetvertnyh@gmail.com 而不是使用问题跟踪器。

贡献

请参阅 贡献 了解详细信息。

致谢

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件