mardi 2 juillet 2019

How can we send multiple notifications in laravel

I want to send a multiple notifications. Like that i send a notification that

you have an event coming up this week

but i want if there is multiple events than the notification will show like that

you have couple of events coming up this week

How this thing i can do that. Please help me.

Here is the code of my controller:

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);
}

In this code it only sends single notification

You have an event coming up this week

But in events if it has multiple events than i want to send this notification

you have couple of events coming up this week

Here is the notification code of upcomingWeekEvents.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 [
        //
    ];
}

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire