laravel-notification-channels/apn 没有发送通知或者我的 iPhone 没有收到通知

laravel-notification-channels/apn either isn't sending notification or my iPhone isn't receiving the notification

using this package 尝试在我的 Laravel 应用程序中发送 apn 通知。但是,我已经按照主页上的文档进行操作,当我尝试发送 apn 通知时,我可以登录调用构造函数和 via 方法的服务器,但我无法弄清楚为什么我的通知不是'未发送或未收到。我的日志也没有来自包裹的信息。

如何解决这个问题?我错过了什么?

MyNotification.php

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Log;
use NotificationChannels\Apn\ApnChannel;
use NotificationChannels\Apn\ApnMessage;

class MyNotification extends Notification
{
    use Queueable;

    public function __construct()
    {
        Log::debug('MyNotification constructor called');
    }

    public function via($notifiable)
    {
        Log::debug('MyNotification via called');
        return [ApnChannel::class];
    }

    public function toApn($notifiable)
    {
        Log::debug('MyNotification toApn called');
        return ApnMessage::create()
            ->badge(1)
            ->title('My title')
            ->body('My body');
    }

    public function routeNotificationForApn($notifiable)
    {
        Log::debug('MyNotification routeNotificationForApn called');
        return $notifiable->token;
    }
}

MyController.php

中的使用代码
    public function sendNotification(MyModel $model)
    {
    // authorization checks here...

        $devices = Device::where('user_id', $model->user_id)->get();
        Notification::send($devices, new MyNotification());

        return response()->json(null, 200);
    }

这是我的 broadcasting.php 和 .env 文件的样子:

您的通知使用 Queueable,因此仅当您的 queue 设置正确时才会发送您的通知。

您可以通过 运行ning

在本地 运行 您的队列
$ php artisan queue:work

在您的控制台上,如果您的排队作业(您的通知)已成功提交(如果它已成功交付则不会),您也会收到反馈。

这里面也高亮了Laravel docs

Before queueing notifications you should configure your queue and start a worker.

如果你需要一个大概是……的队列。您需要决定,因为只有您知道应用程序的负载有多大,应该发送多少通知。

摘自文档:

Sending notifications can take time, especially if the channel needs to make an external API call to deliver the notification. To speed up your application's response time, let your notification be queued by adding the ShouldQueue interface and Queueable trait to your class.

个人意见:让它在没有队列的情况下工作,f。前任。本地,然后添加队列。

如果您使用 Redis 配置 运行 队列,我强烈建议您使用 Laravel Horizon 来监控队列中的作业。

使用 APN 的技巧

config/broadcasting.php 中正确配置 APN 服务 - 请参阅 Github docs

问题是函数 routeNotificationForApn() 属于可通知的 模型 (在我的实例中,Device),而不是 MyNotification class.

如果您没有设置队列,也需要删除 use Queueable;