You are here

pwa_webpush.module in Progressive Web App 7.2

File

modules/pwa_webpush/pwa_webpush.module
View source
<?php

/**
 * @file
 */
define('PWA_WEBPUSH_AUTOMATIC_PADDING_LIMIT', 2847);

// Include composer dependencies here to make it available everywhere.
if (!class_exists('\\Minishlink\\WebPush\\WebPush')) {
  $folders = [
    // The vendor repository will be at the PWA module root.
    __DIR__ . '/../..',
    DRUPAL_ROOT,
    DRUPAL_ROOT . '/..',
  ];
  foreach ($folders as $folder) {
    if (file_exists($folder . '/vendor/autoload.php')) {
      require $folder . '/vendor/autoload.php';
      break;
    }
  }
}

/**
 * Implements hook_permission().
 */
function pwa_webpush_permission() {
  return [
    'access pwa webpush' => [
      'title' => t('Access to PWA Push notifications'),
    ],
    'administer pwa webpush' => [
      'title' => t('Administer PWA Push notifications'),
      'restrict access' => TRUE,
    ],
  ];
}

/**
 * Implements hook_menu().
 */
function pwa_webpush_menu() {
  $items = [];

  // Callback to save the subscription URL to the DB.
  $items['pwa/webpush/subscription/%'] = [
    'title' => t('Manage webpush subscription'),
    'page callback' => 'pwa_webpush_register',
    'page arguments' => [
      3,
    ],
    'access callback' => '_pwa_webpush_save_access',
    'access arguments' => [
      'access pwa webpush',
      [
        'POST',
        'DELETE',
      ],
    ],
    'delivery callback' => 'pwa_deliver_json',
    'file' => 'pwa_webpush.pages.inc',
    'type' => MENU_CALLBACK,
  ];

  // Admin settings to set VAPID informations.
  $items['admin/config/pwa/notification'] = [
    'title' => t('Push notification'),
    'description' => t('Configure Web Push notification settings.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => [
      'pwa_webpush_admin_configuration',
    ],
    'access arguments' => [
      'administer pwa webpush',
    ],
    'file' => 'pwa_webpush.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  ];
  $items['admin/config/pwa/notification/pushapi'] = [
    'title' => t('Push API'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -1,
  ] + $items['admin/config/pwa/notification'];
  $items['admin/config/pwa/notification/pushapi/test'] = [
    'title' => t('Send test notification'),
    'description' => t('Test sending a single push notification.'),
    'page arguments' => [
      'pwa_webpush_send_notification',
    ],
    'type' => MENU_LOCAL_ACTION,
    'weight' => 1,
  ] + $items['admin/config/pwa/notification'];

  // Add a shortcut to send a test notification on the subscription list page.
  $items['admin/config/pwa/notification/subscriptions/test'] = [
    'page callback' => 'drupal_goto',
    'page arguments' => [
      'admin/config/pwa/notification/pushapi/test',
      [
        'query' => [
          'destination' => 'admin/config/pwa/notification/subscriptions',
        ],
      ],
    ],
  ] + $items['admin/config/pwa/notification/pushapi/test'];
  return $items;
}

/**
 * Implements hook_library().
 */
function pwa_webpush_library() {
  $path = drupal_get_path('module', 'pwa_webpush');
  $module_version = pwa_version_assets('pwa_webpush');
  return [
    // Main script with all the subscription logic
    'webpush' => [
      'version' => $module_version,
      'js' => [
        $path . '/js/register.js' => [
          'scope' => 'footer',
        ],
      ],
      'dependencies' => [
        [
          'pwa',
          'register',
        ],
        [
          'pwa',
          'sha256',
        ],
      ],
    ],
    // Handle anonymous subscriptions differently for security and performance.
    'anonymous' => [
      'version' => $module_version,
      'js' => [
        $path . '/js/anonymous.js' => [
          'scope' => 'footer',
        ],
      ],
      'dependencies' => [
        [
          'pwa_webpush',
          'webpush',
        ],
      ],
    ],
    // Script for the UI to enable notifications.
    'button' => [
      'version' => $module_version,
      'js' => [
        $path . '/js/button.js' => [
          'scope' => 'footer',
        ],
      ],
      'dependencies' => [
        [
          'pwa_webpush',
          'webpush',
        ],
      ],
    ],
    // Forcibly register users when permission is already granted.
    'autoregister' => [
      'version' => $module_version,
      'js' => [
        $path . '/js/autoregister.js' => [
          'scope' => 'footer',
        ],
      ],
      'dependencies' => [
        [
          'pwa_webpush',
          'webpush',
        ],
      ],
    ],
  ];
}

/**
 * Implements hook_views_api().
 */
function pwa_webpush_views_api() {
  return [
    'api' => 3,
    'path' => drupal_get_path('module', 'pwa_webpush') . '/views',
  ];
}

/**
 * Implements hook_pwa_serviceworker_script_alter().
 *
 * Add the notification handling script to the serviceworker.
 */
function pwa_webpush_pwa_serviceworker_script_alter(&$scripts) {
  $scripts['notifications'] = drupal_get_path('module', 'pwa_webpush') . '/js/serviceworker/notifications.js';
}

/**
 * Check the HTTP method as well as the permission.
 *
 * @param $perm
 *
 * @return bool
 */
function _pwa_webpush_save_access($perm, $methods) {
  if (!in_array($_SERVER['REQUEST_METHOD'], $methods)) {
    return FALSE;
  }
  return _pwa_access_check($perm);
}
function pwa_webpush_list_subscriptions(array $uids, $options = []) {
  $options += [
    'expired' => 0,
  ];
  $query = db_select('pwa_webpush_subscription', 'pws')
    ->fields('pws', [
    'sid',
    'uid',
    'created',
    'expired',
    'last_used',
    'endpoint_sha256',
    'json',
  ])
    ->condition('uid', $uids);
  foreach ($options as $key => $option) {
    $query
      ->condition($key, $option);
  }
  return $query
    ->execute()
    ->fetchAllAssoc('sid');
}
use Minishlink\WebPush\WebPush;
use Minishlink\WebPush\Subscription;

/**
 * Sends a notification to a list of users.
 *
 *
 * @param array $notification
 *   An object that allows configuring the notification. It can have the following properties:
 *    - actions: An array of actions to display in the notification. The members of the array should be an object
 *      literal. It may contain the following values:
 *      - action: A DOMString identifying a user action to be displayed on the notification.
 *      - title: A DOMString containing action text to be shown to the user.
 *      - icon: A USVString containing the URL of an icon to display with the action.
 *      Appropriate responses are built using event.action within the notificationclick event.
 *   - badge: a USVString containing the URL of an image to represent the notification when there is not enough space
 *     to display the notification itself such as for example, the Android Notification Bar. On Android devices, the badge should accommodate devices up to 4x resolution, about 96 by 96 px, and the image will be automatically masked.
 *   - body: A string representing an extra content to display within the notification.
 *   - data: Arbitrary data that you want to be associated with the notification. This can be of any data type.
 *   - dir : The direction of the notification; it can be auto,  ltr or rtl
 *   - icon: a USVString containing the URL of an image to be used as an icon by the notification.
 *   - image: a USVString containing the URL of an image to be displayed in the notification.
 *   - lang: Specify the lang used within the notification. This string must be a valid BCP 47 language tag.
 *   - renotify: A boolean that indicates whether to suppress vibrations and audible alerts when reusing a tag value.
 *     If options’s renotify is true and options’s tag is the empty string a TypeError will be thrown.
 *     The default is false.
 *   - requireInteraction: Indicates that on devices with sufficiently large screens, a notification should remain
 *     active until the user clicks or dismisses it. If this value is absent or false, the desktop version of Chrome
 *     will auto-minimize notifications after approximately twenty seconds. The default value is false.
 *   - silent: When set indicates that no sounds or vibrations should be made. If options’s silent is true and
 *     options’s vibrate is present a TypeError exception will be thrown. The default value is false.
 *   - tag: An ID for a given notification that allows you to find, replace, or remove the notification using a script
 *     if necessary.
 *   - timestamp: A DOMTimeStamp representing the time when the notification was created. It can be used to indicate
 *     the time at which a notification is actual. For example, this could be in the past when a notification is used
 *     for a message that couldn’t immediately be delivered because the device was offline, or in the future for a
 *     meeting that is about to start.
 *   - vibrate: A vibration pattern to run with the display of the notification. A vibration pattern can be an array
 *     with as few as one member. The values are times in milliseconds where the even indices (0, 2, 4, etc.)
 *     indicate how long to vibrate and the odd indices indicate how long to pause. For example, [300, 100, 400] would
 *     vibrate 300ms, pause 100ms, then vibrate 400ms.
 *
 * @param array $uids
 * @param array $options
 *
 * @throws \ErrorException
 * @throws \InvalidMergeQueryException
 * @throws \Exception
 *
 * @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification
 */
function pwa_webpush_send($notification, array $uids, array $options = []) {
  $auth = [
    'VAPID' => [
      'subject' => variable_get('pwa_webpush_vapid_subject'),
      'publicKey' => variable_get('pwa_webpush_vapid_public'),
      'privateKey' => variable_get('pwa_webpush_vapid_private'),
    ],
  ];
  $webpush_options = [
    'TTL' => (int) variable_get('pwa_webpush_notification_ttl', NULL),
    'batchSize' => (int) variable_get('pwa_webpush_notification_batchsize', NULL),
  ];
  foreach ([
    'urgency',
    'topic',
  ] as $param) {
    $webpush_options[$param] = variable_get('pwa_webpush_notification_' . strtolower($param), NULL);
  }
  $webpush = new WebPush($auth, array_filter($webpush_options));
  $webpush
    ->setAutomaticPadding((int) variable_get('pwa_webpush_notification_padding', PWA_WEBPUSH_AUTOMATIC_PADDING_LIMIT));
  $webpush
    ->setReuseVAPIDHeaders(TRUE);
  $user_subs = pwa_webpush_list_subscriptions($uids, $options);

  // send multiple notifications with payload
  $payload = drupal_json_encode($notification);
  foreach ($user_subs as $record) {
    $webpush
      ->queueNotification(Subscription::create(drupal_json_decode($record->json)), $payload);
  }
  foreach ($webpush
    ->flush() as $report) {
    _pwa_webpush_subscription_update($report);
  }
  drupal_set_message('Notification sent to ' . count($user_subs) . ' users');
}

/**
 *
 * @param \Minishlink\WebPush\MessageSentReport $report
 *
 * @throws \InvalidMergeQueryException
 */
function _pwa_webpush_subscription_update($report) {
  $key = [
    'endpoint_sha256' => hash('sha256', $report
      ->getEndpoint()),
  ];
  $fields = [];
  if ($report
    ->isSuccess()) {
    $fields['last_used'] = REQUEST_TIME;
  }
  if ($report
    ->isSubscriptionExpired()) {
    $fields['expired'] = REQUEST_TIME;
    watchdog('webpush', 'Notification subscription expired <pre>@sub</pre>', [
      '@sub' => json_encode($report
        ->jsonSerialize(), JSON_PRETTY_PRINT),
    ]);
  }
  if (!empty($fields)) {
    db_merge('pwa_webpush_subscription')
      ->key($key)
      ->fields($fields)
      ->execute();
  }
}

/**
 * Implements hook_block_info().
 */
function pwa_webpush_block_info() {
  $blocks = [];
  $blocks['pwa_webpush_register'] = [
    'info' => t('PWA register notifications'),
  ];
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function pwa_webpush_block_view($delta = '') {
  $block = [];
  if ($delta === 'pwa_webpush_register') {
    $text = variable_get('pwa_webpush_block_register_label', t('Enable notifications'));
    $user_subs = [];

    // Get a list of keys of active subscriptions for this user.
    if (user_is_logged_in()) {
      $user_subs = array_map(function ($sub) {
        return $sub->endpoint_sha256;
      }, pwa_webpush_list_subscriptions([
        $GLOBALS['user']->uid,
      ]));
    }
    $block['content'] = [
      '#access' => _pwa_access_check('access pwa webpush'),
      '#attached' => [
        'js' => [
          [
            'data' => [
              'pwa' => [
                'webpush' => [
                  'applicationServerKey' => variable_get('pwa_webpush_vapid_public', NULL),
                  'userSubscriptions' => (object) $user_subs,
                ],
              ],
            ],
            'type' => 'setting',
          ],
        ],
      ],
      // UI to manage subscription to notifications.
      'button' => [
        'text' => [
          '#type' => 'markup',
          '#markup' => '<button type="button" data-drupal-pwa-webpush-register disabled hidden>' . $text . '</button>',
        ],
        '#attached' => [
          'library' => [
            [
              'pwa_webpush',
              'button',
            ],
          ],
        ],
      ],
      // For anonymous users the subscription code is a little different.
      'anonymous' => [
        '#access' => user_is_anonymous(),
        '#attached' => [
          'library' => [
            [
              'pwa_webpush',
              'anonymous',
            ],
          ],
        ],
      ],
      // React to permission changes.
      'autoregister' => [
        '#access' => variable_get('pwa_webpush_autoregister', TRUE),
        '#attached' => [
          'library' => [
            [
              'pwa_webpush',
              'autoregister',
            ],
          ],
        ],
      ],
    ];
  }
  return $block;
}

/**
 * Implements hook_block_configure().
 *
 * This hook declares configuration options for blocks provided by this module.
 */
function pwa_webpush_block_configure($delta = '') {
  $form = [];
  if ($delta === 'pwa_webpush_register') {
    $form['pwa_webpush_block_register_label'] = [
      '#type' => 'textfield',
      '#title' => t('Label of the register button'),
      '#default_value' => variable_get('pwa_webpush_block_register_label', t('Enable notifications')),
    ];
  }
  return $form;
}

/**
 * Implements hook_block_save().
 */
function pwa_webpush_block_save($delta = '', $edit = array()) {
  if ($delta === 'pwa_webpush_register') {
    variable_set('pwa_webpush_block_register_label', $edit['pwa_webpush_block_register_label']);
  }
}

/**
 * Send a simple message alert to a single account.
 */
function pwa_webpush_send_message_account($account, $title, $body, $url, $icon) {
  if (!is_object($account) || !is_numeric($account->uid)) {
    watchdog('pwa_webpush', 'Not a valid account object.');
    return FALSE;
  }
  $notification = [
    'title' => $title,
    'body' => $body,
    'icon' => $icon,
    'data' => [
      'url' => $url,
    ],
  ];
  pwa_webpush_send($notification, [
    $account->uid,
  ]);
}

Functions

Namesort descending Description
pwa_webpush_block_configure Implements hook_block_configure().
pwa_webpush_block_info Implements hook_block_info().
pwa_webpush_block_save Implements hook_block_save().
pwa_webpush_block_view Implements hook_block_view().
pwa_webpush_library Implements hook_library().
pwa_webpush_list_subscriptions
pwa_webpush_menu Implements hook_menu().
pwa_webpush_permission Implements hook_permission().
pwa_webpush_pwa_serviceworker_script_alter Implements hook_pwa_serviceworker_script_alter().
pwa_webpush_send Sends a notification to a list of users.
pwa_webpush_send_message_account Send a simple message alert to a single account.
pwa_webpush_views_api Implements hook_views_api().
_pwa_webpush_save_access Check the HTTP method as well as the permission.
_pwa_webpush_subscription_update

Constants