Listen to WonderPushSubscriptionEvent

sdk

#1

Hi,

I want to be able to send my installation id and subscription state to my database once the user is subscribed after been asked. Is there a way to listen to WonderPushSubscriptionEvent instead of getting the subscription state with the getSubscriptionState()?

Let me explain why : I ask the user using WonderPushSDK.setNotificationEnabled(true) and then call WonderPushSDK.getInstallationId() to get the installation id. However, most of the time I get a undefined as the user did not authorize push notifications yet.

By listening to the WonderPushSubscriptionEvent, I would be able to get the installation id only when it’s available or handle other subscription states such as denial.

EDIT : I’ve found on your demo page that it is possible to listen to WonderPushEvent on the window. However, I’m unable to catch it with window.addEventListener('WonderPushEvent', function(e) {}

EDIT 2 : Here is my full event listener on the ‘WonderPushEvent’

window.addEventListener('WonderPushEvent', function(e) {
    var evt = e.originalEvent || {};
    var detail = evt.detail || {};
	    
    if (detail.name === 'subscription') {
        var subscriptionState = detail.state;
	    switch (subscriptionState) {
	        case WonderPushSDK.SubscriptionState.UNSUPPORTED:
	            console.log('UNSUPPORTED');
	            break;
	        case WonderPushSDK.SubscriptionState.UNDETERMINED:
	        case WonderPushSDK.SubscriptionState.NOT_SUBSCRIBED:
            case WonderPushSDK.SubscriptionState.UNSUBSCRIBED:
		    console.log('not yet');
		    break;
	        case WonderPushSDK.SubscriptionState.SUBSCRIBED:
		    console.log('ok');
                break;
           }
    }
});

Thanks in advance,


#2

Hi Victor,

See the optin-switch plugin code for reference.

You should probably listen for this event before initializing the SDK.

Can you provide a quick repro?

Best,


#3

Here is a partial repo with all my files related to push notifications.

As it’s a java webapp, we don’t use plain javascript directly into our JSP (as it’s not reusable and a good practice). So I created a tag JSTL which when included in a JSP will be replaced by its content ( META-INF > tags > kpush-autorisation.tag ). kpush-autorisation.js is loaded alongside this tag. I use this to send a form with the token to my server.

I separated your copy-paste loading script into different files which are loaded on every page of the website (the order in which they are loaded is the following) :

  • WonderPushSDK-cdn-loader.js : it’s your minimized js code which load the SDK
  • WonderPushSDK_fr.js : optInOptions in French
  • js-init-WonderPushSDK : .init() the SDK

The tag will just add a button on my page, if I click on it, it calls setNotificationEnabled(true) (it works). I want to retrieve the installationId not too early, once the user is subscribed (thus listening to the event of subscription).

UPDATE
Looks like I’ve found the solution : in your demo, you are using JQuery’s .originalEvent with var event = evt.originalEvent || {} . Because I don’t use JQuery, it was always {}.