[9.x] Fix queueable notification's ID overwritten#42581
Conversation
When the notification ID has been assigned, It should not be overwritten. Even in the queue.
|
How are you defining a special unique ID that is being overwritten? |
@taylorotwell Thanks for your attention My cases like this: Part 1: <?php
namespace App\Supports;
use Str;
class UniqueID
{
/**
* Generates a string UID.
*/
public static function generateString(): string
{
return str_replace('-', '', (string) Str::orderedUuid());
}
}Part 2: <?php
namespace App\Notifications;
use App\Enums\EntityAlias;
use App\Enums\InteractType;
use App\Models\User;
use App\Supports\UniqueID;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class UserInteractsNotification extends Notification
{
use Queueable;
private EntityAlias $topic = EntityAlias::User;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(
private InteractType $type,
private User $fromUser,
) {
$this->id = UniqueID::generateString();
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database'];
}
// ...
}Part 3: The important thing is... When queue driver is framework/src/Illuminate/Notifications/NotificationSender.php Lines 138 to 146 in 3b8d805 When notification should queue, the method NotificationSender::queueNotification() will be called, and it does not have a determination of whether the ID exists or not.
So I added same code to line 194 framework/src/Illuminate/Notifications/NotificationSender.php Lines 188 to 197 in 3b8d805 |
When the notification ID has been assigned, It should not be overwritten. Even in the queue.
The case for database notification:
I've defined a special unique ID, but it's overwritten here.
If this option is not desirable, the
Str::uuid()should also be replace toStr::orderedUuid()for keep order of database index.