Skip to content

@jakguru/vueprint / @jakguru/vueprint/services/push / PushService

Class: PushService

@jakguru/vueprint/services/push.PushService

The Push service handles the integration between the application and the browser API's for Push / Desktop notifications and the service workers which are used to enable background push notifications.

Accessing the Push Service

The Push Service is both injectable and accessible from the global Vue instance:

vue

<script lang="ts">
import { defineComponent, inject } from 'vue'
import type { PushService } from '@jakguru/vueprint'
export default defineComponent({
    setup() {
        const push = inject<PushService>('push')
        return {}
    }
    mounted() {
        const push: PushService = this.config.globalProperties.$push
    }
})
</script>

Using the Push Service

Determining Push Permission State

Using the accessor PushService.canPush and PushService.canRequestPermission, you can determine if the visitor has already permitted push notifications (or web push notifications) or if the application is allowed to request those permissions. This can be used to display a prompt in the UI to request permissions for Push notifications.

It is also possible to activate the permission flow of the browser by triggering PushService.requestPushPermission method, or to request that the application disable the PushService.canRequestPermission from returning true by triggering the PushService.doNotRequestPushPermission method.

It is also possible to manually trigger a desktop notification by calling the PushService.createWebPushNotification method.

Constructors

constructor

new PushService(bus, ls, cron, identity, api, firebaseOptions, onAuthenticatedForFirebase, onUnauthenticatedForFirebase, serviceWorkerPath?, serviceWorkerMode?): PushService

Create a new PushService instance.

Parameters

NameTypeDescription
busBusServiceThe BusService instance to use for communication
lsLocalStorageServiceThe LocalStorageService instance to use for storing and retrieving preferences and tokens
cronMiliCronThe MiliCron instance to use for scheduling updates
identityIdentityServiceThe IdentityService instance to use for determining if the user is authenticated
apiApiService-
firebaseOptionsFirebaseOptionsThe options to use for initializing Firebase
onAuthenticatedForFirebaseFirebaseTokenAuthenticationCallbackThe callback to use for storing the Firebase Messaging Token in an external service when the user is authenticated
onUnauthenticatedForFirebaseFirebaseTokenAuthenticationCallbackThe callback to use for removing the Firebase Messaging Token from an external service when the user is unauthenticated
serviceWorkerPath?null | stringThe path to the service worker to use for handling push notifications
serviceWorkerMode?null | "classic" | "module"The mode to use for the service worker

Returns

PushService

Defined in

src/services/push.ts:175

Accessors

appUpdatePending

get appUpdatePending(): ComputedRef<boolean>

If there is an update pending for the service worker.

Returns

ComputedRef<boolean>

Defined in

src/services/push.ts:293


booted

get booted(): Ref<boolean>

Whether or not the service has been booted.

Returns

Ref<boolean>

Defined in

src/services/push.ts:265


canPush

get canPush(): ComputedRef<null | boolean>

Whether or not permissions have been granted for push notifications.

Returns

ComputedRef<null | boolean>

Defined in

src/services/push.ts:279


canRequestPermission

get canRequestPermission(): ComputedRef<boolean>

Whether or not the UI should show a prompt for the user to allow push notifications.

Returns

ComputedRef<boolean>

Defined in

src/services/push.ts:272


serviceWorkerState

get serviceWorkerState(): ComputedRef<ServiceWorkerState>

The current state of the service worker.

Returns

ComputedRef<ServiceWorkerState>

Defined in

src/services/push.ts:286

Methods

boot

boot(): void

Boot the service.

Returns

void

Defined in

src/services/push.ts:429


createWebPushNotification

createWebPushNotification(options): void

Create a web push notification (otherwise known as a desktop notification).

Parameters

NameTypeDescription
optionsWebPushNotificationOptionsThe options for the web push notification

Returns

void

Defined in

src/services/push.ts:331


doNotRequestPushPermission

doNotRequestPushPermission(): void

Stop asking the user for permission to show push notifications.

Returns

void

void

Defined in

src/services/push.ts:319


requestPushPermission

requestPushPermission(): void

Request permission to show push notifications.

Returns

void

Defined in

src/services/push.ts:300


shutdown

shutdown(): void

Shut down the service.

Returns

void

Defined in

src/services/push.ts:605


update

update(): Promise<void>

Returns

Promise<void>

Defined in

src/services/push.ts:640

Vueprint is a commercial work product released under the MIT License and is provided as-is with no warranty or guarantee of support.