Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking β€œSign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎟️ πŸ”” πŸ“± Custom Events + pushNotify + storeDeviceSettings + remoteNotify rewrites #1093

Merged
Merged
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
ea3fb10
create event handler
Oct 31, 2023
d267fcf
change file name
Oct 31, 2023
7d69847
rewrite pushNotify in react
Oct 31, 2023
56156a0
remove references to old file
Oct 31, 2023
332bfbe
centralize the event names
Oct 31, 2023
a991814
emit AND publish events
Oct 31, 2023
a90af2f
add function descriptions
Oct 31, 2023
09cbaa1
ensure proper initialization
Oct 31, 2023
b1838aa
add tests for customEventHandler
Oct 31, 2023
a6bf881
Merge remote-tracking branch 'origin/startprefs-rewrite' into event-n…
Oct 31, 2023
2dca41e
finish resolving merge conflicts
Oct 31, 2023
bb8f1ec
add rough tests, correct data access
Nov 1, 2023
5fbeac2
increase tests
Nov 1, 2023
c406988
pushnotify relies on events now
Nov 1, 2023
d27aedd
update tests
Nov 1, 2023
82e864b
add docstrings to customEventHandler
Nov 1, 2023
0eb7b4b
test cloud event
Nov 1, 2023
24784db
add timeout to config mock
Nov 1, 2023
46d5ed8
update tests
Nov 1, 2023
4f5e853
sync up test runs
Nov 1, 2023
38c64c4
Merge remote-tracking branch 'upstream/service_rewrite_2023' into eve…
Nov 2, 2023
e1999fd
rename file
Nov 2, 2023
ede2347
rewrite service to typscript
Nov 2, 2023
cd6dc05
remove / update references
Nov 2, 2023
7b7efc8
add initStoreDeviceSettings call
Nov 2, 2023
78eb704
add more docstrings
Nov 2, 2023
72a76fa
add server comm mock
Nov 2, 2023
188d478
start tests
Nov 2, 2023
395940f
resolve prettier merge conflicts
Nov 2, 2023
5e04536
re-prettify merge conflicted files
Nov 2, 2023
8764701
Merge remote-tracking branch 'upstream/service_rewrite_2023' into eve…
Nov 2, 2023
4393362
prettify recent changes
Nov 2, 2023
bc5c589
update tests so they pass
Nov 3, 2023
1aceb94
complete tests
Nov 3, 2023
9581ca2
remove unused mock code
Nov 3, 2023
9aa89d5
rename remotenotify file
Nov 3, 2023
16c87f0
typescript migration
Nov 3, 2023
68e0701
formatting changes to controller.js
Nov 3, 2023
84064bb
remove angular.isDefined calls
Nov 3, 2023
b58ee5c
clean up comments and docstrings
Nov 3, 2023
87de6c2
first two tests function
Nov 3, 2023
dcf35fa
complete writing testing
Nov 3, 2023
9b1665a
simplify event name storage
Nov 6, 2023
c25ebb6
directly use alert, for now
Nov 6, 2023
ad68e1b
remove old alerts
Nov 6, 2023
85d2884
swap var for const
Nov 6, 2023
29f44e7
update test
Nov 6, 2023
77d2ad1
Merge branch 'service_rewrite_2023' into event-notif-rewrite
shankari Nov 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rewrite pushNotify in react
updating to React, including the event subscription model usage
  • Loading branch information
Abby Wheelis committed Oct 31, 2023
commit 7d69847752ce448f94c7a70cfdcd7e31b6d77e60
303 changes: 150 additions & 153 deletions www/js/splash/pushNotifySettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,176 +15,173 @@

import angular from 'angular';
import { updateUser } from '../commHelper';
import { logDebug, displayError } from '../plugin/logger';
import { publish, subscribe, EVENT_NAMES } from '../customEventHandler';
import { getAngularService } from '../angular-react-helper';

angular.module('emission.splash.pushnotify', ['emission.plugin.logger',
'emission.services',
'emission.splash.startprefs'])
.factory('PushNotify', function($window, $state, $rootScope, $ionicPlatform,
$ionicPopup, Logger, StartPrefs) {
let push = null;

var pushnotify = {};
var push = null;
pushnotify.CLOUD_NOTIFICATION_EVENT = 'cloud:push:notification';

pushnotify.startupInit = function() {
push = $window.PushNotification.init({
"ios": {
"badge": true,
"sound": true,
"vibration": true,
"clearBadge": true
},
"android": {
"iconColor": "#008acf",
"icon": "ic_mood_question",
"clearNotifications": true
const startupInit = function () {
push = window['PushNotification'].init({
"ios": {
"badge": true,
"sound": true,
"vibration": true,
"clearBadge": true
},
"android": {
"iconColor": "#008acf",
"icon": "ic_mood_question",
"clearNotifications": true
}
});
push.on('notification', function (data) {
if (window['cordova'].platformId == 'ios') {
// Parse the iOS values that are returned as strings
if (angular.isDefined(data) &&
angular.isDefined(data.additionalData)) {
if (angular.isDefined(data.additionalData.payload)) {
data.additionalData.payload = JSON.parse(data.additionalData.payload);
}
});
push.on('notification', function(data) {
if ($ionicPlatform.is('ios')) {
// Parse the iOS values that are returned as strings
if(angular.isDefined(data) &&
angular.isDefined(data.additionalData)) {
if(angular.isDefined(data.additionalData.payload)) {
data.additionalData.payload = JSON.parse(data.additionalData.payload);
}
if(angular.isDefined(data.additionalData.data) && typeof(data.additionalData.data) == "string") {
data.additionalData.data = JSON.parse(data.additionalData.data);
} else {
console.log("additionalData is already an object, no need to parse it");
}
} else {
Logger.log("No additional data defined, nothing to parse");
}
if (angular.isDefined(data.additionalData.data) && typeof (data.additionalData.data) == "string") {
data.additionalData.data = JSON.parse(data.additionalData.data);
} else {
console.log("additionalData is already an object, no need to parse it");
}
$rootScope.$emit(pushnotify.CLOUD_NOTIFICATION_EVENT, data);
});
} else {
logDebug("No additional data defined, nothing to parse");
}
}
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, data);
});
}

pushnotify.registerPromise = function() {
return new Promise(function(resolve, reject) {
pushnotify.startupInit();
push.on("registration", function(data) {
console.log("Got registration " + data);
resolve({token: data.registrationId,
type: data.registrationType});
});
push.on("error", function(error) {
console.log("Got push error " + error);
reject(error);
});
console.log("push notify = "+push);
});
}
const registerPromise = function () {
return new Promise(function (resolve, reject) {
startupInit();
push.on("registration", function (data) {
console.log("Got registration " + data);
resolve({
token: data.registrationId,
type: data.registrationType
});
});
push.on("error", function (error) {
console.log("Got push error " + error);
reject(error);
});
console.log("push notify = " + push);
});
}

pushnotify.registerPush = function() {
pushnotify.registerPromise().then(function(t) {
// alert("Token = "+JSON.stringify(t));
Logger.log("Token = "+JSON.stringify(t));
return $window.cordova.plugins.BEMServerSync.getConfig().then(function(config) {
return config.sync_interval;
}, function(error) {
console.log("Got error "+error+" while reading config, returning default = 3600");
return 3600;
}).then(function(sync_interval) {
updateUser({
device_token: t.token,
curr_platform: ionic.Platform.platform(),
curr_sync_interval: sync_interval
});
return t;
});
}).then(function(t) {
// alert("Finished saving token = "+JSON.stringify(t.token));
Logger.log("Finished saving token = "+JSON.stringify(t.token));
}).catch(function(error) {
Logger.displayError("Error in registering push notifications", error);
const registerPush = function () {
registerPromise().then(function (t) {
// alert("Token = "+JSON.stringify(t));
logDebug("Token = " + JSON.stringify(t));
return window['cordova'].plugins.BEMServerSync.getConfig().then(function (config) {
return config.sync_interval;
}, function (error) {
console.log("Got error " + error + " while reading config, returning default = 3600");
return 3600;
}).then(function (sync_interval) {
updateUser({
device_token: t['token'],
curr_platform: window['cordova'].platformId,
curr_sync_interval: sync_interval
});
}
return t;
});
}).then(function (t) {
// alert("Finished saving token = "+JSON.stringify(t.token));
logDebug("Finished saving token = " + JSON.stringify(t.token));
}).catch(function (error) {
displayError(error, "Error in registering push notifications");
});
}

var redirectSilentPush = function(event, data) {
Logger.log("Found silent push notification, for platform "+ionic.Platform.platform());
if (!$ionicPlatform.is('ios')) {
Logger.log("Platform is not ios, handleSilentPush is not implemented or needed");
// doesn't matter if we finish or not because platforms other than ios don't care
return;
}
Logger.log("Platform is ios, calling handleSilentPush on DataCollection");
var notId = data.additionalData.payload.notId;
var finishErrFn = function(error) {
Logger.log("in push.finish, error = "+error);
};
const redirectSilentPush = function (event, data) {
logDebug("Found silent push notification, for platform " + window['cordova'].platformId);
if (window['cordova'].platformId != 'ios') {
logDebug("Platform is not ios, handleSilentPush is not implemented or needed");
// doesn't matter if we finish or not because platforms other than ios don't care
return;
}
logDebug("Platform is ios, calling handleSilentPush on DataCollection");
var notId = data.additionalData.payload.notId;
var finishErrFn = function (error) {
logDebug("in push.finish, error = " + error);
};

pushnotify.datacollect.getConfig().then(function(config) {
if(config.ios_use_remote_push_for_sync) {
pushnotify.datacollect.handleSilentPush()
.then(function() {
Logger.log("silent push finished successfully, calling push.finish");
showDebugLocalNotification("silent push finished, calling push.finish");
push.finish(function(){}, finishErrFn, notId);
})
} else {
Logger.log("Using background fetch for sync, no need to redirect push");
push.finish(function(){}, finishErrFn, notId);
};
window['cordova'].plugins.BEMDataCollection.getConfig().then(function (config) {
if (config.ios_use_remote_push_for_sync) {
window['cordova'].plugins.BEMDataCollection.handleSilentPush()
.then(function () {
logDebug("silent push finished successfully, calling push.finish");
showDebugLocalNotification("silent push finished, calling push.finish");
push.finish(function () { }, finishErrFn, notId);
})
.catch(function(error) {
push.finish(function(){}, finishErrFn, notId);
Logger.displayError("Error while redirecting silent push", error);
});
}
} else {
logDebug("Using background fetch for sync, no need to redirect push");
push.finish(function () { }, finishErrFn, notId);
};
})
.catch(function (error) {
push.finish(function () { }, finishErrFn, notId);
displayError(error, "Error while redirecting silent push");
});
}

var showDebugLocalNotification = function(message) {
pushnotify.datacollect.getConfig().then(function(config) {
if(config.simulate_user_interaction) {
cordova.plugins.notification.local.schedule({
id: 1,
title: "Debug javascript notification",
text: message,
actions: [],
category: 'SIGN_IN_TO_CLASS'
});
}
});
var showDebugLocalNotification = function (message) {
window['cordova'].plugins.BEMDataCollection.getConfig().then(function (config) {
if (config.simulate_user_interaction) {
window['cordova'].plugins.notification.local.schedule({
id: 1,
title: "Debug javascript notification",
text: message,
actions: [],
category: 'SIGN_IN_TO_CLASS'
});
}
});
}

pushnotify.registerNotificationHandler = function() {
$rootScope.$on(pushnotify.CLOUD_NOTIFICATION_EVENT, function(event, data) {
Logger.log("data = "+JSON.stringify(data));
if (data.additionalData["content-available"] == 1) {
redirectSilentPush(event, data);
}; // else no need to call finish
});
};
const onCloudEvent = function (event, data) {
logDebug("data = " + JSON.stringify(data));
if (data.additionalData["content-available"] == 1) {
redirectSilentPush(event, data);
}; // else no need to call finish
}

$ionicPlatform.ready().then(function() {
pushnotify.datacollect = $window.cordova.plugins.BEMDataCollection;
StartPrefs.readConsentState()
.then(StartPrefs.isConsented)
.then(function(consentState) {
if (consentState == true) {
pushnotify.registerPush();
} else {
Logger.log("no consent yet, waiting to sign up for remote push");
}
});
pushnotify.registerNotificationHandler();
Logger.log("pushnotify startup done");
});
const onConsentEvent = function (event, data) {
const StartPrefs = getAngularService('StartPrefs');
console.log("got consented event " + JSON.stringify(event['name'])
+ " with data " + JSON.stringify(data));
if (StartPrefs.isIntroDone()) {
console.log("intro is done -> reconsent situation, we already have a token -> register");
registerPush();
}
}

$rootScope.$on(StartPrefs.CONSENTED_EVENT, function(event, data) {
console.log("got consented event "+JSON.stringify(event.name)
+" with data "+ JSON.stringify(data));
if (StartPrefs.isIntroDone()) {
console.log("intro is done -> reconsent situation, we already have a token -> register");
pushnotify.registerPush();
const onIntroEvent = function (event, data) {
console.log("intro is done -> original consent situation, we should have a token by now -> register");
registerPush();
}

const initPushNotify = function () {
const StartPrefs = getAngularService('StartPrefs');
StartPrefs.readConsentState()
.then(StartPrefs.isConsented)
.then(function (consentState) {
if (consentState == true) {
registerPush();
} else {
logDebug("no consent yet, waiting to sign up for remote push");
}
});

$rootScope.$on(StartPrefs.INTRO_DONE_EVENT, function(event, data) {
console.log("intro is done -> original consent situation, we should have a token by now -> register");
pushnotify.registerPush();
});
subscribe(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, (event) => onCloudEvent(event, event['detail'].data));
subscribe(StartPrefs.CONSENTED_EVENT, (event) => onConsentEvent(event, event['detail'].data));
subscribe(StartPrefs.INTRO_DONE_EVENT, (event) => onIntroEvent(event, event['detail'].data));

return pushnotify;
});
logDebug("pushnotify startup done");
}