You are here

function pwa_webpush_send in Progressive Web App 7.2

Sends a notification to a list of users.

Parameters

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.

array $uids:

array $options:

Throws

\ErrorException

\InvalidMergeQueryException

\Exception

See also

https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistrati...

2 calls to pwa_webpush_send()
pwa_webpush_send_message_account in modules/pwa_webpush/pwa_webpush.module
Send a simple message alert to a single account.
pwa_webpush_send_notification_submit in modules/pwa_webpush/pwa_webpush.admin.inc
_state

File

modules/pwa_webpush/pwa_webpush.module, line 242

Code

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');
}