我们如何在Laravel中发送多个通知

时间:2019-07-02 08:28:18

标签: php laravel laravel-5 push-notification notifications

我想发送多个通知。像这样,我发送一条通知

  

您本周有一个活动

但是我想要是否有多个事件,通知将像这样显示

  

您本周有几项活动

这件事我怎么能做到。请帮助我。

这是我的控制器的代码:

public function upcomingWeekEvent()
{
    $datatime = new DateTime();     // getting current date and time
    $currentDatetime = $datatime->format('Y-m-d');
    $datatime->add(new DateInterval('P1W'));    // getting 1 week after of current time period
    $afterWeekDatetime = $datatime->format('Y-m-d');

    $events = Activity::where('activity_type', 'event')
        ->whereBetween('activity_datetime_from', [$currentDatetime, $afterWeekDatetime])->get();
        // dd($events);

    foreach ($events as $event) {
        $user = $event->user;

        $push = new PushNotification('apn');
        $push->setMessage([
            'aps' => [
                'alert' => ' You have an event coming up this week',
                'sound' => 'default',
                'badge' =>  $user->unreadNotifications->count()

            ],
            'extraPayLoad' => [
            // 'user' => $authUser,
            'event' => $event->id,
            'activity_type' => $event->activity_type,
            'notification_type' => "UpcomingWeekEvent",
            ]
        ]);
        $tokens = $user->deviceTokens()->pluck('deviceToken')->toArray();
        $push->setDevicesToken($tokens);
        $push->send();
        $feedback = $push->getFeedback();

        $user->notify(new UpcomingWeekEvent($event));
    }
    return response()->json(['aasa'=>$events]);
    // return response()->json(['currentDatetime'=> $currentDatetime, 'afterWeekDatetime'=> $afterWeekDatetime, 'result'=> $events], 200);
}

在此代码中,它仅发送单个通知

  

这周你要举办一个活动

但是在事件中,如果有多个事件比我想要发送此通知

  

您本周有几项活动

这是即将到来的WeekEvents.php的通知代码     

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class UpcomingWeekEvent extends Notification
{
use Queueable;

protected $event;

/**
 * Create a new notification instance.
 *
 * @return void
 */
public function __construct($event)
{
    $this->event = $event;
}

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

/**
 * Get the database representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function toDatabase($notifiable)
{
    return [
        'activity_id' => $this->event->id,
        'notification' => 'You have an event coming up this week',
        'icon_type' => $this->event->activity_type,
    ];
}

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

}

0 个答案:

没有答案