@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:
<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
Name | Type | Description |
---|---|---|
bus | BusService | The BusService instance to use for communication |
ls | LocalStorageService | The LocalStorageService instance to use for storing and retrieving preferences and tokens |
cron | MiliCron | The MiliCron instance to use for scheduling updates |
identity | IdentityService | The IdentityService instance to use for determining if the user is authenticated |
api | ApiService | - |
firebaseOptions | FirebaseOptions | The options to use for initializing Firebase |
onAuthenticatedForFirebase | FirebaseTokenAuthenticationCallback | The callback to use for storing the Firebase Messaging Token in an external service when the user is authenticated |
onUnauthenticatedForFirebase | FirebaseTokenAuthenticationCallback | The callback to use for removing the Firebase Messaging Token from an external service when the user is unauthenticated |
serviceWorkerPath? | null | string | The path to the service worker to use for handling push notifications |
serviceWorkerMode? | null | "classic" | "module" | The mode to use for the service worker |
Returns
Defined in
Accessors
appUpdatePending
• get
appUpdatePending(): ComputedRef
<boolean
>
If there is an update pending for the service worker.
Returns
ComputedRef
<boolean
>
Defined in
booted
• get
booted(): Ref
<boolean
>
Whether or not the service has been booted.
Returns
Ref
<boolean
>
Defined in
canPush
• get
canPush(): ComputedRef
<null
| boolean
>
Whether or not permissions have been granted for push notifications.
Returns
ComputedRef
<null
| boolean
>
Defined in
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
serviceWorkerState
• get
serviceWorkerState(): ComputedRef
<ServiceWorkerState
>
The current state of the service worker.
Returns
ComputedRef
<ServiceWorkerState
>
Defined in
Methods
boot
▸ boot(): void
Boot the service.
Returns
void
Defined in
createWebPushNotification
▸ createWebPushNotification(options
): void
Create a web push notification (otherwise known as a desktop notification).
Parameters
Name | Type | Description |
---|---|---|
options | WebPushNotificationOptions | The options for the web push notification |
Returns
void
Defined in
doNotRequestPushPermission
▸ doNotRequestPushPermission(): void
Stop asking the user for permission to show push notifications.
Returns
void
void
Defined in
requestPushPermission
▸ requestPushPermission(): void
Request permission to show push notifications.
Returns
void
Defined in
shutdown
▸ shutdown(): void
Shut down the service.
Returns
void
Defined in
update
▸ update(): Promise
<void
>
Returns
Promise
<void
>