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

Notification actions support #5

Open
jtsoya539 opened this issue Mar 18, 2021 · 9 comments
Open

Notification actions support #5

jtsoya539 opened this issue Mar 18, 2021 · 9 comments

Comments

@jtsoya539
Copy link

Hi @vertonghenb!
Thanks for this great package! 💯

Any chance to add support for notification actions following NotificationAction APIs?

@MrGeneric
Copy link

Agreed, great package.

I've run into this limitation too. I tried to add support, but ran into an issue I'm not sure how to get around. After adding Actions to the Notification object I get this error:

Failed to construct 'Notification': Actions are only supported for persistent notifications shown using ServiceWorkerRegistration.showNotification().

I'm using Blazor server, so not using ServiceWorkers. I think it should be possible to get this to work if you're using WASM.

@vertonghenb
Copy link
Contributor

@jtsoya539 Good idea, any idea how to implement it?

@sho9029
Copy link

sho9029 commented Aug 29, 2021

@vertonghenb
I found a way to use the NotificationAction API from C#. (using service worker)

Implement the Action class.

public class Action
{
    public string Action { get; set; }
    public string Title { get; set; }
    public string Icon { get; set; }
}

inject

[Inject]
private INotificationService _notificationService { get; set; }

then, I did this (using the anonymous type here, but originally using the NotificationOptions class)

var title = "Title";
var options = new
{
    // Put other options here.
    actions = new List<Action>
    {
        new Action { Action = "action1", Title = "Action1", Icon = "icon url" },
        new Action { Action = "action2", Title = "Action2", Icon = "icon url" },
        // You can add as much as Notification.maxActions
    }
};

await _notificationService.CreateAsync(title, options);

read more about Notification.maxActions

however, in my environment, in order to achieve this, I had to rewrite the JavaScript side code as well.

export async function create(title, options) {
    const reg = await navigator.serviceWorker.getRegistration();
    reg.showNotification(title, options);
}

Since it uses ServiceWorker, I can't pullRequest my implementation of NotificationAction to this repository.

@xDaniel17
Copy link

Hi, @vertonghenb, Thank you very much for your project.

I have a question, why https://agreeable-rock-071180b03.azurestaticapps.net/ does not work in android and the version https://web-push-book.gauntface.com/demos/notification-examples/ yes.. any difference in the code?

Error:
blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Failed to construct 'Notification': Illegal constructor. Use ServiceWorkerRegistration.showNotification() instead.
TypeError: Failed to construct 'Notification': Illegal constructor. Use ServiceWorkerRegistration.showNotification() instead.

Thanks.

@sho9029
Copy link

sho9029 commented Sep 12, 2021

Hi, @xDaniel17.

I know answers to that question, so I will answer it for you.

https://agreeable-rock-071180b03.azurestaticapps.net/ uses this Notification constructor, which is not supported by Android (Chrome and WebView).
However, https://web-push-book.gauntface.com/demos/notification-examples/ uses a service worker method as shown in this site.
Why this error occurs when using the constructor in Android is described in this site.
(to summarize, the browser on Android does not support constructors.)

@xDaniel17
Copy link

Thank you so much for your response @sho9029 !

@bennimoser
Copy link

@sho9029 Have you found a Solution for catching the Action Click? "notificationclick" doesnt seem to work

@sho9029
Copy link

sho9029 commented Nov 18, 2022

@bennimoser If you use firebase, this comment may be resolve your problem.

@dikshatickoo1991
Copy link

Hi. I am facing issue. Click event is not getting generated

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

7 participants