Using JavaScript callbacks during subscription process
Notix publisher can observe user behavior during subscription process. All significant events that happen can produce JavaScript callbacks. Website owner can perform different actions for different users with the use of these callbacks basing on users’ subscription statuses.
Live example
Here you can see in realtime list of callbacks called on this page. Source code of this example is below.
Possible callback list
onPermissionAllowed
This function is called when web site visitor grants permissions to receive push notifications
or these permissions were granted earlier
Typical reasons that lead to this event:
- user presses "Allow" button
- user grants permissions to send notifications for all web sites
onPermissionDenied
This function is called when web site visitor denies permissions to receive push notifications
or these permissions were rejected/denied earlier
Typical reasons that lead to this event:
- user presses "Deny" button
- user presses "close" button a few times
- user declines receiving push notifications (and ask permissions) for all sites
onAlreadySubscribed
This function is called when web site page loads and visitor has already granted permissions for receiving push notifications. It's called just after page loading, without any user actions.
onNotificationUnsupported
This function is called when web site visitor uses browser that is incompatible with push notifications.
This could be caused by:
- using old browser
- viewing web site using something like Smart TV or other "smart" device
- using rare/exotic browser without implementation of required APIs
onPermissionDefault
This function is called when user presses "cross" on permission dialog, which closes permission request dialog without accepting or declining.
onPermissionCanAsk
This is shorthand for mixing other callbacks. This function is called when publisher can ask permissions (they are not denied or granted or unsupported). This callback is not called when subscription is disabled programmatically using sdk.softUnsubscribe() method (see onSubscriptionDisabled)
onPermissionCanNotAsk
This is shorthand for mixing other callbacks. This function is called when publisher can not ask permissions (they are already denied or granted or unsupported). This callback is not called when subscription is disabled programmatically using sdk.softUnsubscribe() method (see onSubscriptionDisabled)
onSubscriptionEnabled
Is called when user granted permissions for receiving push notifications and didn't unsubscribed. It's normal live subscription that will receive push messages. Usage example can be found here or here.
onSubscriptionDisabled
Is called when user has permissions for domain, but subscription is disabled with sdk.softUnsubscribe() method. Usage example can be found here or here.
Example
This quite simple example shows how to call a piece of JavaScript code when some event happens with user. Example of hiding some page elements based on subscription state can be found here.
<script> const s = document.createElement("script") s.src = "/tag/enot.min.js" s.onload = (sdk) => { sdk.startInstall({ appId: "YOUR-APP-ID-HERE", loadSettings: true, step0: "skip", }); sdk.SDKCaller.onPermissionAllowed(() => { log("onPermissionAllowed callback") }); sdk.SDKCaller.onPermissionDenied(() => { log("onPermissionDenied callback") }); sdk.SDKCaller.onAlreadySubscribed(() => { log("onAlreadySubscribed callback") }); sdk.SDKCaller.onNotificationUnsupported(() => { log("onNotificationUnsupported callback") }); sdk.SDKCaller.onPermissionDefault(() => { log("onPermissionDefault callback") }); sdk.SDKCaller.onPermissionCanAsk(() => { log("onPermissionCanAsk callback") }); sdk.SDKCaller.onPermissionCanNotAsk(() => { log("onPermissionCanNotAsk callback") }); sdk.SDKCaller.onSubscriptionEnabled(() => { log("onSubscriptionEnabled callback") }); sdk.SDKCaller.onSubscriptionDisabled(() => { log("onSubscriptionDisabled callback") }); } document.head.append(s) </script>