• DevSolved
  • Custom Notification Don't Appear in the Interface

Log:
AH01071: Got error 'PHP message: PHP Warning: Attempt to read property "id" on array in /var/www/vhosts/example.com/example.com/vendor/sycho/json-api/src/AbstractSerializer.php on line 38; PHP message: PHP Warning: Attempt to read property "id" on array in /var/www/vhosts/example.com/example.com/vendor/sycho/json-api/src/AbstractSerializer.php on line 38'

Curl Respond:
HTTP/1.1 201 Created
Server: nginx
Date: Thu, 23 Jan 2025 10:50:29 GMT
Content-Type: application/vnd.api+json
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/8.2.27
X-CSRF-Token: ***
Set-Cookie: flarum_session=***; Path=/; Expires=Thu, 23 Jan 2025 12:50:29 GMT; Max-Age=7200; Secure; HttpOnly; SameSite=Lax
Cache-Control: max-age=0
Expires: Thu, 23 Jan 2025 10:50:29 GMT
X-Powered-By: PleskLin

{"data":{"type":"notifications","id":"","attributes":{"message":"Bu bir test bildirimidir!","status":"Notifications sent successfully","userIds":[1,2]}}}

Body:
{
"data": {
"type": "notifications",
"id": "",
"attributes": {
"message": "Bu bir test bildirimidir!",
"status": "Notifications sent successfully",
"userIds": [1, 2]
}
}
}

SQL:

data:

Issue:
I am developing an extension that allows sending special notifications to desired members via API from the administration panel.

Everything seems to be working fine, but custom notifications are not appearing in the forum interface.

  • huseyinfiliz Not entirely sure, but the problem you're facing may be the following:

    add the following in your blueprint getData

    public function getData()
        {
            return [
                // We want these notifications to always be sent, even if they received one before.
                'uuid' => (string) Str::orderedUuid(),
            ];
        }

    This should get documented somewhere.

    When the notification syncer tries to send a notification, it first deletes what seems to be the exact same notifications received before to prevent duplicates. This works fine in normal notification contexts (involving a specific post or discussion ..etc)

    But in the unique context of actual repeated notifications that look the same based on

    (type, from_user_id, subject_id, data)
    ---
    ("customNotification", null, 1, {"message":"same message"})

    then it would delete the previous ones every time.

Yes, I think the notification is marked as deleted when the recipient and sender are the same.

However, when I send a notification to a different user, the notification count increases, but it doesn't appear.

I hadn't understood that I needed to make a definition within src/forum. I figured it out by examining other extensions with notification features. However, I still don't know how to prevent notifications from the same person to the same person from being deleted.

SychO

I thought the issue was notifications coming from the same ID, but I was actually trying to send notifications to all members at once via the API. During testing, there were 2 members, and the first member, i.e., ID 1, had their notifications appearing as deleted. When a new member was added, I noticed that the notifications for members with IDs 1 and 2 were deleted, while the notifications for the member with ID 3 were still visible.

What is your recommendation for identifying the issue?

Here is my cURL command:

curl -X POST "https://example.com/api/notifications/send" \
-H "Authorization: Token <token>" \
-H "Content-Type: application/json" \
-d '{
    "message": "Test 2",
    "userIds": [1, 2, 3]
}'

    huseyinfiliz Not entirely sure, but the problem you're facing may be the following:

    add the following in your blueprint getData

    public function getData()
        {
            return [
                // We want these notifications to always be sent, even if they received one before.
                'uuid' => (string) Str::orderedUuid(),
            ];
        }

    This should get documented somewhere.

    When the notification syncer tries to send a notification, it first deletes what seems to be the exact same notifications received before to prevent duplicates. This works fine in normal notification contexts (involving a specific post or discussion ..etc)

    But in the unique context of actual repeated notifications that look the same based on

    (type, from_user_id, subject_id, data)
    ---
    ("customNotification", null, 1, {"message":"same message"})

    then it would delete the previous ones every time.

      SychO
      It took me a while to understand what you meant. At first, I wasted time trying to add the user ID, but then I realized I needed to use a unique identifier. Instead of dealing with the user ID, I tried using random bytes, and it seems like my problem is solved!

      Thank you for your help

          public function getData(): array
          {
              return [
                  'type' => $this->notificationType,
                  'message' => $this->message,
                  'uniqe' => bin2hex(random_bytes(1)),
              ];
          }

      Demo;

        huseyinfiliz It took me a while to understand what you meant. At first, I wasted time trying to add the user ID, but then I realized I needed to use a unique identifier.

        sorry, I meant literally "(string) Str::orderedUuid()" but didn't include the import statement.

        use Illuminate\Support\Str;
        
        (string) Str::orderedUuid()

        can also use Str::random(), but your solution works as well.

          SychO Ah, I really misunderstood. I'm quite new to Laravel (coming back to it after years). Now I'm going back to apply the first method you suggested. I'm truly grateful!