Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Payload / message should have an index 'notification' #170

Closed
marydelapedra opened this issue Jan 27, 2022 · 3 comments
Closed

Payload / message should have an index 'notification' #170

marydelapedra opened this issue Jan 27, 2022 · 3 comments

Comments

@marydelapedra
Copy link

marydelapedra commented Jan 27, 2022

The notification does not appear if you are going to use the angular/pwa package as a service worker because in the payload, the main requirement is it should have one root object containing one property named notification, otherwise the messages will not be displayed to the user.

I think it is better that the developer will define the message freely inside the custom notification class? I have to hack the package specifically the WebPushChannel class, send function and add an index notification on the payload variable. Is there any other way to do this?
image

@poruchik85
Copy link

poruchik85 commented Jun 24, 2022

I change my PushNotification:
from

public function toWebPush($notifiable, $notification): WebPushMessage
{
    return (new WebPushMessage)
        ->icon('img/logo.png')
        ->title('Test message')
        ->body($this->msg);
}

to

public function toWebPush($notifiable, $notification): WebPushMessage
{
    $msg = (new WebPushMessage);
    $msg->notification = [
        'icon' => 'img/logo.png',
        'title' => 'Test message',
        'body' => $this->msg,
    ];
    return $msg;
}

This is workaround, but it works

@imrodrigoalves
Copy link

imrodrigoalves commented Oct 26, 2022

Hey,

First thanks for noticing the Angular issue was eventuality hit that.

Second I'd not recommend changing the source code of the library since you end up needing to maintain your own version of it.

I'd suggest extending the base WebPushMessage like below.

<?php

namespace App\Notifications\Models;

use Illuminate\Support\Arr;
use NotificationChannels\WebPush\WebPushMessage;

class AngularWebPushMessage extends WebPushMessage {
    public function toArray()
    {
        return ['notification' => parent::toArray()];
    }
}

Using this approach you retain all previously provided functionality.

public function toWebPush($notifiable, $notification)
    {
        return (new AngularWebPushMessage)
            ->title('Approved!')
            ->icon('/approved-icon.png')
            ->body('Your account was approved!')
            ->action('View account', 'view_account')
            ->options(['TTL' => 1000]);
    }

Output:

{
  "notification": {
    "title": "Approved!",
    "actions": [{ "title": "View account", "action": "view_account" }],
    "body": "Your account was approved!",
    "icon": "/approved-icon.png"
  }
}

@joostdebruijn
Copy link
Collaborator

Thanks for your suggestion @imrodrigoalves! I think that's a proper solution for this issue. If someone has a suggestion for a better solution within this package, just open a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants