trin4ik/laravel-expo-push

Laravel Expo 推送通知

v0.2.0 2023-03-30 19:42 UTC

This package is auto-updated.

Last update: 2024-08-30 01:06:26 UTC


README

Latest Version on Packagist Total Downloads

一个简单而愚蠢的包,用于使用 expo-notification 服务 发送推送通知。
没有测试,没有从客户端获取令牌等的路由/控制器。只有通道。
如果需要更多,请使用 Alymosul/laravel-exponent-push-notifications

安装

composer require trin4ik/laravel-expo-push
php artisan vendor:publish --provider "Trin4ik\LaravelExpoPush\ExpoPushServiceProvider"

您可以将查询日志记录到数据库中,包含有效载荷和响应。

echo "EXPO_PUSH_LOG=true" >> .env
php artisan migrate

用户 Expo 令牌

您需要为任何模型添加方法 routeNotificationForExpoPush,例如 User,该模型返回 Expo 令牌

<?php

namespace App\Models;

// ...

class User extends Authenticatable
{
    use Notifiable;

    // ...

    public function routeNotificationForExpoPush () {
        return $this->expo_token; // like ExponentPushToken[XXXXXXX_XXXXXXXXXXXXXX]
    }
}

更改日志数据库

默认情况下,日志写入到 expo_push_notification 表,您可以在以下步骤中更改它

  1. 扩展 Trin4ik\LaravelExpoPush\Models\ExpoPushNotification.php 如下
<?php
namespace App\Models;

class ExpoPushNotification extends Trin4ik\LaravelExpoPush\Models\ExpoPushNotification {
    protected $table = 'my_custom_notifications_table';
}
  1. config/expo-push.php 中更改 log/driver/instance 为您的新模型
return [
    // ...
    'log' => [
        // ...
        'driver' => [
            // ...
            'instance' => \App\Models\ExpoPushNotification::class
        ]
    ]
];

用法

与其他通道一样,您需要创建 Notification

php artisan make:notification PushTest
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Trin4ik\LaravelExpoPush\Channels\ExpoPushChannel;
use Trin4ik\LaravelExpoPush\ExpoPush;

class PushTest extends Notification implements ShouldQueue
{
    use Queueable;
    
    public function via($notifiable)
    {
        return [ExpoPushChannel::class];
    }

    public function toExpoPush($notifiable) {
        return ExpoPush::create()
            ->badge(1)
            ->title("Congratulations!")
            ->body("Your " . $notifiable->email . " account was approved!");
    }
}

有关 ExpoPush::create 方法的更多信息,请 向下查看

用法如下

<?php
use App\Notifications\PushTest;
use App\Models\User;

// ...

$user = User::find(1);
$user->notify(new PushTest);

ExpoPush::create 方法

有关 Expo 通知服务消息格式 的更多信息

如果新 SDK 发布了新的格式,您可以通过创建 PR 或在您的 Notification 中使用 toArray() 方法来处理

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Trin4ik\LaravelExpoPush\Channels\ExpoPushChannel;

class PushTest extends Notification implements ShouldQueue
{
    use Queueable;
    
    public function via($notifiable)
    {
        return [ExpoPushChannel::class];
    }

    public function toArray($notifiable) {
        return [
            'badge' => 1,
            'title' => "Congratulations!",
            'body' => "Your " . $notifiable->email . " account was approved!",
            'new_expo_notification_param' => true
        ];
    }
}

更改日志

请参阅 CHANGELOG 了解最近更改的信息。

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全性

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

鸣谢

许可

MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。

感谢

❤️ 此包是使用 Laravel Package Boilerplate 生成的。
❤️ 更多代码来自 Alymosul/laravel-exponent-push-notifications