Skip to content

Commit

Permalink
Merge pull request #4 from CleverTap/bugfix/SDK-991/vapid_fix
Browse files Browse the repository at this point in the history
bugfix(SDK-991): VAPID not getting generated on first App Launched
  • Loading branch information
darshanclevertap authored Oct 11, 2021
2 parents 3d5f563 + ed4fcf7 commit 5c7eb6b
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 25 deletions.
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
Version 1.0.0 *(4 January, 2018)*
-------------------------------------------
Change Log
==========
Version 1.1.1 *(11 October, 2021)*
---------------------------------
- Fixes a bug where VAPID token was not being pushed to CleverTap on first App Launch

Initial release.
Version 1.0.0 *(4 January, 2018)*
---------------------------------
- Initial release.
4 changes: 3 additions & 1 deletion Examples/Simple/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ window.addEventListener('DOMContentLoaded', function() {

clevertap.setLogLevel(clevertap.logLevels.DEBUG);
clevertap.setAppVersion("1.0.0");
clevertap.init('WWW-WWW-WWRZ');
clevertap.init('566-7Z4-W54Z');
console.log(clevertap.getCleverTapID());
clevertap.registerCTNotifications();

var apiURL = 'https://developer.mozilla.org/search.json?q=';
var errorMsg = document.getElementById('error');
Expand Down
11 changes: 10 additions & 1 deletion Examples/Simple/manifest.webapp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@
"type": "privileged",
"permissions": {
"systemXHR": {
"description": "Required to load remote content"
"description": "Required to load remote content"
},
"serviceworker": {
"description": "Needed for associating service worker"
},
"desktop-notification": {
"description": "Required for displaying alerts when app is on background"
},
"push": {
"description": "To receive notifications"
}
},
"installs_allowed_from": [
Expand Down
1 change: 1 addition & 0 deletions Examples/Simple/serviceWorker.js
64 changes: 56 additions & 8 deletions clevertap.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var Constants = {
ECOOKIE_PREFIX: 'CT_E',
GCOOKIE_NAME: 'CT_G',
VAPID_KEY: 'CT_VKEY',
VAPID_STATE_KEY: 'CT_VKEY_STATE',
KCOOKIE_NAME: 'CT_K',
PCOOKIE_PREFIX: 'CT_P',
SEQCOOKIE_PREFIX: 'CT_SEQ',
Expand Down Expand Up @@ -705,6 +706,15 @@ var StorageManager = function () {
}
return Constants.CHARGED_ID + '_' + accountId;
}
}, {
key: 'getVAPIDStateKey',
value: function getVAPIDStateKey() {
var accountId = Account.getAccountId();
if (!accountId) {
return null;
}
return Constants.VAPID_STATE_KEY + '_' + accountId;
}
}]);
return StorageManager;
}();
Expand Down Expand Up @@ -1044,6 +1054,22 @@ var Device = function () {
StorageManager.save(GUIDKey, guid);
}
}
}, {
key: 'getVAPIDState',
value: function getVAPIDState() {
var VAPIDStateKey = StorageManager.getVAPIDStateKey();
return StorageManager.read(VAPIDStateKey);
}
}, {
key: 'setVAPIDState',
value: function setVAPIDState(vapidState) {
var VAPIDStateKey = StorageManager.getVAPIDStateKey();
if (vapidState === null) {
StorageManager.remove(VAPIDStateKey);
} else {
StorageManager.save(VAPIDStateKey, vapidState);
}
}
}, {
key: 'getGUID',
value: function getGUID() {
Expand Down Expand Up @@ -2112,7 +2138,7 @@ var Request = function () {
return Request;
}();

var version = 10000;
var version = 10101;

var _loadSavedUnsentEvents = function _loadSavedUnsentEvents(unsentKey) {
var savedUnsentEvents = StorageManager.read(unsentKey);
Expand Down Expand Up @@ -2312,6 +2338,13 @@ var QueueManager = function () {
Utils$1.log.debug('kaios vapid recieved: ' + response.KVAPID);
Device.setVAPID(response.KVAPID);
}
for (var i = 0; i < events.length; i++) {
var event = events[i];
if (event.evtName === Constants.APP_LAUNCHED) {
Device.setVAPIDState(true);
}
}

if (response.hasOwnProperty('kaiosPush')) {
Utils$1.log.debug('kaios notification status: ' + response.kaiosPush);
Device.setKaiOsNotificationState(response.kaiosPush);
Expand Down Expand Up @@ -2804,7 +2837,7 @@ var CleverTapAPI = function () {

var publicVapidKey = Device.getVAPID();
var subscriptionData = {};
console.log("subscribing push notification sw " + publicVapidKey);
console.log("Base 64 VAPID Key " + this.urlBase64ToUint8Array(publicVapidKey));
swRegistration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: this.urlBase64ToUint8Array(publicVapidKey)
Expand All @@ -2817,6 +2850,8 @@ var CleverTapAPI = function () {
_this5._startuploadPushToken(subscriptionData);
var curTs = new Date().getTime();
Device.setLastTokenUpdateTs(curTs);
}).catch(function (error) {
console.error('Subcription error ', error);
});
}
}, {
Expand Down Expand Up @@ -3171,6 +3206,9 @@ var CleverTap = function () {
}
Account.setAccountId(id);
Account.setRegion(region);
if (Device.getVAPIDState() === null) {
Device.setVAPIDState(false);
}
this.api = new CleverTapAPI(Object.assign({}, this.options));
this.session = new SessionHandler(this.api);
this.user = new UserHandler(this.api);
Expand Down Expand Up @@ -3212,6 +3250,8 @@ var CleverTap = function () {
}, {
key: '_registerCTNotifications',
value: function _registerCTNotifications(serviceWorkerPath, unregister) {
var _this = this;

if (!serviceWorkerPath) {
serviceWorkerPath = this.swpath;
} else {
Expand All @@ -3220,15 +3260,23 @@ var CleverTap = function () {
Utils$1.log.debug('register initiated, vapid: ' + Device.getVAPID());

// kaios-Vapid and Push Notification on dashboard should be enabled
if (Device.getVAPID() && Device.getKaiOsNotificationState()) {
if (this.api !== null) {
Utils$1.log.debug('registering SW callled');
this.api.registerSW(serviceWorkerPath, unregister);
console.log('Device VAPID State ' + Device.getVAPIDState());
if (Device.getVAPIDState()) {
if (Device.getVAPID() && Device.getKaiOsNotificationState()) {
if (this.api !== null) {
Utils$1.log.debug('registering SW callled');
this.api.registerSW(serviceWorkerPath, unregister);
} else {
Utils$1.log.debug('clevertap-Api context not available ' + this.api);
}
} else {
Utils$1.log.debug('clevertap-Api context not available ' + this.api);
Utils$1.log.debug('Service Worker Subscription from client failed: Vapid-key: ' + Device.getVAPID() + ' Notification Enabled:' + Device.getKaiOsNotificationState());
}
} else {
Utils$1.log.debug('Service Worker Subscription from client failed: Vapid-key: ' + Device.getVAPID() + ' Notification Enabled:' + Device.getKaiOsNotificationState());
Utils$1.log.debug('setting timeout and calling _registerCTNotifications again in 2s');
setTimeout(function () {
_this._registerCTNotifications(serviceWorkerPath, unregister);
}, 2000);
}
}
}, {
Expand Down
2 changes: 1 addition & 1 deletion clevertap.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clevertap-fxos",
"version": "1.1.0",
"version": "1.1.1",
"description": "CleverTap Firefox OS SDK",
"main": "clevertap.js",
"scripts": {
Expand Down
26 changes: 19 additions & 7 deletions src/clevertap.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export default class CleverTap {
}
Account.setAccountId(id);
Account.setRegion(region);
if(Device.getVAPIDState() === null){
Device.setVAPIDState(false);
}
this.api = new CleverTapAPI(Object.assign({}, this.options));
this.session = new SessionHandler(this.api);
this.user = new UserHandler(this.api);
Expand Down Expand Up @@ -62,16 +65,25 @@ export default class CleverTap {
Utils.log.debug('register initiated, vapid: ' + Device.getVAPID());

// kaios-Vapid and Push Notification on dashboard should be enabled
if(Device.getVAPID() && Device.getKaiOsNotificationState()) {
if(this.api !== null) {
Utils.log.debug('registering SW callled');
this.api.registerSW(serviceWorkerPath,unregister);
console.log('Device VAPID State '+ Device.getVAPIDState());
if(Device.getVAPIDState()){
if(Device.getVAPID() && Device.getKaiOsNotificationState()) {
if(this.api !== null) {
Utils.log.debug('registering SW callled');
this.api.registerSW(serviceWorkerPath,unregister);
} else {
Utils.log.debug('clevertap-Api context not available ' + this.api);
}
} else {
Utils.log.debug('clevertap-Api context not available ' + this.api);
Utils.log.debug('Service Worker Subscription from client failed: Vapid-key: ' + Device.getVAPID() + ' Notification Enabled:' + Device.getKaiOsNotificationState());
}
} else {
Utils.log.debug('Service Worker Subscription from client failed: Vapid-key: ' + Device.getVAPID() + ' Notification Enabled:' + Device.getKaiOsNotificationState());
}else{
Utils.log.debug('setting timeout and calling _registerCTNotifications again in 2s');
setTimeout(() => {
this._registerCTNotifications(serviceWorkerPath,unregister);
},2000);
}

}
unregisterCTNotifications(serviceWorkerPath) {
this._registerCTNotifications(serviceWorkerPath,true);
Expand Down
6 changes: 4 additions & 2 deletions src/clevertapAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export default class CleverTapAPI {
triggerPushSubscription(swRegistration) {
let publicVapidKey = Device.getVAPID();
var subscriptionData = {} ;
console.log("subscribing push notification sw " + publicVapidKey);
console.log("Base 64 VAPID Key " + this.urlBase64ToUint8Array(publicVapidKey));
swRegistration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: this.urlBase64ToUint8Array(publicVapidKey)
Expand All @@ -245,7 +245,9 @@ export default class CleverTapAPI {
this._startuploadPushToken(subscriptionData);
var curTs = new Date().getTime();
Device.setLastTokenUpdateTs(curTs);
});
}).catch(function(error) {
console.error('Subcription error ', error);
});
}

urlBase64ToUint8Array(base64String) {
Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default {
ECOOKIE_PREFIX: 'CT_E',
GCOOKIE_NAME: 'CT_G',
VAPID_KEY: 'CT_VKEY',
VAPID_STATE_KEY: 'CT_VKEY_STATE',
KCOOKIE_NAME: 'CT_K',
PCOOKIE_PREFIX: 'CT_P',
SEQCOOKIE_PREFIX: 'CT_SEQ',
Expand Down
12 changes: 12 additions & 0 deletions src/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ export default class Device {
StorageManager.save(GUIDKey, guid);
}
}
static getVAPIDState(){
const VAPIDStateKey = StorageManager.getVAPIDStateKey();
return StorageManager.read(VAPIDStateKey);
}
static setVAPIDState(vapidState){
const VAPIDStateKey = StorageManager.getVAPIDStateKey();
if(vapidState === null){
StorageManager.remove(VAPIDStateKey);
}else{
StorageManager.save(VAPIDStateKey,vapidState);
}
}
static getGUID() {
if (!_guid) {
const GUIDKey = StorageManager.getGUIDKey();
Expand Down
7 changes: 7 additions & 0 deletions src/queueManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ export default class QueueManager {
Utils.log.debug(`kaios vapid recieved: ${response.KVAPID}`);
Device.setVAPID(response.KVAPID);
}
for(var i = 0; i < events.length; i++){
var event = events[i];
if(event.evtName === Constants.APP_LAUNCHED){
Device.setVAPIDState(true);
}
}

if (response.hasOwnProperty('kaiosPush')){
Utils.log.debug(`kaios notification status: ${response.kaiosPush}`);
Device.setKaiOsNotificationState(response.kaiosPush);
Expand Down
7 changes: 7 additions & 0 deletions src/storageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,11 @@ export default class StorageManager {
}
return `${Constants.CHARGED_ID}_${accountId}`;
}
static getVAPIDStateKey(){
const accountId = Account.getAccountId();
if(!accountId){
return null;
}
return `${Constants.VAPID_STATE_KEY}_${accountId}`;
}
}
2 changes: 1 addition & 1 deletion src/version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default 10000;
export default 10101;

0 comments on commit 5c7eb6b

Please sign in to comment.