Push framework notifications
This module provides web push notifications for Drupal sites, specifically integrating with the DANSE and Push frameworks for Progressive Web Apps. It allows users to subscribe to notifications for events like new comments and receive these alerts directly in their browser.
About
Provides web push notifications for DANSE events. Implements Web Push library for PHP, DANSE and Push framework, tailored and tested - a single case scenario though - to work with PWA.
Many thanks to:
- sebastix for creative contribution and management.
- yfiervil for the inspiration, ideas and code for web_push module.
For requirements see composer.json
Install and configure
- Get module via composer, this will fetch all requirements.
-
Enable module
drush en pf_notifications -
Add this permission to Role(s) who are granted to subscribe to notifications:
Access POST on Push notification subscription resource - Go to Configuration > System > Push framework > Notifications (/admin/config/system/push_framework/pf_notifications) and generate or enter VAPID keys. Tweak any other settings there.
- Try there Test notification too - check "Subscribe" checkbox there before hitting "Send" button.
- If not yet - configure DANSE content for Content and Comment types you wish to implement notifications. Check some resources @sebastix.nl and perhaps this issue.
There is an article written by @Sebastix here: https://sebastix.nl/blog/using-web-push-notifications-with-a-drupal-progressive-web-app/ explaining how to configure this module with DANSE and enable push notifications with the PWA module.
Usage scenario: a very basic example
- First test on config page, test notification found there.
- Login as
user1and post a content that will hold the thread with comments. Then, use DANSE's widget to subscribe for when other users comment there. - In the other browser login as
user2and post a comment on the same content. Subscribe this user for any of available DANSE subscriptions. -
Then, either have advanced queue do its thing on cron runs, or if you have Ultimate cron module you can execute at least two jobs on the page /admin/config/system/cron/jobs. Or run DANSE notifications and PF queues with drush
drush danse:notifications:create
drush pf:sources:collect
drush pf:queue:process
Result: user1 shall get the browser's/device's notification where is logged.
Other usage: direct notifications
To use direct notifications, skipping DANSE and Push framework, it's possible with a module's service. In particular with usage of 2 methods \Drupal::service('pf_notifications.subscription')->prepareLibraries($danse_key, 0, $entity) and
\Drupal::service('pf_notifications.subscription')->subscriptionResponse($op, $danse_key, $entity, $response, 0, FALSE).
See README for a full example.
Hooks
See pf_notifications.api.php for a couple of hooks available.
TODO
- Implement Service worker unregister/update on uninstall. If module is installed again soon user may need to un-subscribe first, to flush existing, now invalid service worker subscription.
- Push notification shall link to DANSE message, to flag it as seen?