laravelpush-notificationonesignallaravel-notificationlaravel-scheduler

Laravel schedule push notification and cancel it if necessary


In my app with Laravel on back-end users can send messages to each other.

I want to send push notification to app users on new inbox message, but I need to send messages only if user hadn't already read this message.

So I see it that way

  1. On every message sended I need to schedule Laravel notification after 1 minute
  2. if user already received this message I need to cancel this notification

How can dismiss scheduled notification in Laravel? Is this approach fine and actual now?

Class extends Notification

public function via($notifiable)
{
    if($this->dontSend($notifiable)) {
        return [];
    }
    return ['mail'];
}

public function dontSend($notifiable)
{
    return $this->appointment->status === 'cancelled';
} 

Maybe there is more convenient way to handle it? For example, to send push every time but somehow dismiss it showing from app if it's already launched?


Solution

  • One way to do it would be something like this;

    $identifier = Str::random(64);
    $delay = now()->addMinute();
    $user->notify((new MyNotification($identifier))->delay($delay));
    // send a request that contains identifier.
    
    Redis::set('SgiA7EfBQBFQK3pjRWtaxB1CkSf7gf4lSixvei3jU3ydHJ39ZGjhhdUUCnHRno3C', 1, 120);
    
    Redis::del('SgiA7EfBQBFQK3pjRWtaxB1CkSf7gf4lSixvei3jU3ydHJ39ZGjhhdUUCnHRno3C');
    
    Redis::exists('SgiA7EfBQBFQK3pjRWtaxB1CkSf7gf4lSixvei3jU3ydHJ39ZGjhhdUUCnHRno3C');
    
    class MyNotification extends BaseNotification
    {
        use Queueable;
    
        private $identifier;
    
        public function __construct($identifier)
        {
            $this->identifier = $identifier;
        }
    
        public function via()
        {
            return $this->isValid() ? ['mail'] : [];
        }
    
        public function isValid()
        {
            return Redis::exists($this->identifier);
        }
    
        public function toMail()
        {
            // details..
        }
    }
    

    It doesn't have to be Redis but it is a perfect match for these kind of key/value structure.