laravel 如何将通知合并到单个通知示例?

2g32fytz  于 2023-03-19  发布在  其他
关注(0)|答案(1)|浏览(112)

我正在尝试为我的应用实现通知功能。每次通过OrderObserver创建Order时,它都会向快递发送OrderReceived通知。
目前的实现工作正常,但有一个问题,当多个用户订购一个项目在同一时间(让我们说100),快递将收到100个通知,这是不好的。
我想知道是否有任何方法可以将这些通知合并到一个通知示例中,这样就不必发送此消息100次:
收到订单!
它应改为发送此通知:
收到100份订单!
我正在考虑使用队列来实现这一点,但当我阅读文档并试图理解时,通过将其排队,它将产生相同的行为,因为它将100个通知示例排队。
下面是我的实现。我可以做些什么来完成它呢?
OrderObserver.php

class OrderObserver
{
    /**
     * Handle the Order "created" event.
     *
     * @param  \App\Models\Order  $order
     * @return void
     */
    public function created(Order $order)
    {
        User::couriers()->each(function ($courier) use ($order) {
            $courier->notify(new OrderReceived($order));
        });
    }
}

OrderReceived.php

class OrderReceived extends Notification
{
    use Queueable;

    /**
     * The received order.
     * 
     * @var Order
     */
    protected Order $order;

    /**
     * The title of this notification.
     * 
     * @var string
     */
    protected string $title;

    /**
     * The subtitle of this notification.
     * 
     * @var string
     */
    protected string $subtitle;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->title = 'Pesanan Diterima!';
        $this->subtitle = 'Ada pesanan baru nih! Yuk cek dimana~';
    }

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

    public function toFcm($notifiable)
    {
        return FcmMessage::create()
            ->setData(['order_id' => $this->order->id])
            ->setNotification(
                \NotificationChannels\Fcm\Resources\Notification::create()
                    ->setTitle($this->title)
                    ->setBody($this->subtitle)
            )
            ->setAndroid(
                AndroidConfig::create()
                    ->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
                    ->setNotification(AndroidNotification::create()->setColor('#0A0A0A'))
            )->setApns(
                ApnsConfig::create()
                    ->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios'))
            );
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->line('The introduction to the notification.')
            ->action('Notification Action', url('/'))
            ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}
q3qa4bjr

q3qa4bjr1#

作为一种解决方案,我决定在客户端使用FCM通知API处理此行为,方法是在OrderReceived通知类中设置collapse_key(Android)和apns-collapse-id(iOS),以便在弹出具有相同键的通知时崩溃。
并将当前行为视为限制,直到我找到更好的解决方案。

/**
 * This enum is stored in another part of the application
 * 
 * Maximum allowed collapse key in android is 4 so we need
 * to create an enum to track how many collapse keys do we
 * have in our app.
 *
 * @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#androidconf
 *
*/
enum CollapseKeys: string
{
  case orderReceived = 'orderReceived';
}

// inside `toFcm` method of `OrderReceived.php`

protected string $collapseKey = CollapseKeys::orderReceived->value;

// android
AndroidConfig::create()->setCollapseKey($this->collapseKey)

// ios
ApnsConfig::create()->setHeaders([
    'apns-collapse-id' => $this->collapseKey
])

如果你们有任何其他可能在服务器端实现的方法,请随意。
我会很高兴的。

相关问题