summaryrefslogtreecommitdiff
path: root/StoneIsland/platforms/android/assets/www/js/lib/etc/push.js
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2018-05-10 17:15:14 +0200
committerJules Laplace <julescarbon@gmail.com>2018-05-10 17:15:14 +0200
commit2fb2e09bb24340a7cc33ae30037f45125791131c (patch)
tree45afccf7b9455ab48f7725a24bae5c28ad02ca9e /StoneIsland/platforms/android/assets/www/js/lib/etc/push.js
parent6196541cf993d5a1a4b8b7f511bf7daf8f849608 (diff)
androidz
Diffstat (limited to 'StoneIsland/platforms/android/assets/www/js/lib/etc/push.js')
-rwxr-xr-xStoneIsland/platforms/android/assets/www/js/lib/etc/push.js150
1 files changed, 150 insertions, 0 deletions
diff --git a/StoneIsland/platforms/android/assets/www/js/lib/etc/push.js b/StoneIsland/platforms/android/assets/www/js/lib/etc/push.js
new file mode 100755
index 00000000..746172eb
--- /dev/null
+++ b/StoneIsland/platforms/android/assets/www/js/lib/etc/push.js
@@ -0,0 +1,150 @@
+var push = (function(){
+ var push = { settings: {}, disabled: false }
+ var pushPlugin
+ push.init = function(){
+ if (! ('device' in window) || (device.platform || "").toLowerCase() !== "ios") {
+ console.log("push disabled")
+ push.disabled = true
+ return
+ }
+
+ pushPlugin = PushNotification.init({
+ ios: {
+ alert: true,
+ badge: true,
+ sound: false,
+ clearBadge: true,
+ },
+ })
+
+ console.log("push init")
+
+ PushNotification.hasPermission(push.did_initialize)
+ pushPlugin.on('registration', push.got_registration)
+ pushPlugin.on('notification', push.got_push_notification)
+ }
+ push.did_initialize = function(data) {
+ // console.log(data)
+ if (! data.isEnabled) {
+ console.log("push did not initialize")
+ return
+ }
+ console.log("push did initialize")
+ var hub_status = localStorage.getItem("yoox.push_hub")
+ var store_status = localStorage.getItem("yoox.push_store")
+
+ push.settings.requested = localStorage.getItem("yoox.push_requested") == "true"
+ push.settings.hub = hub_status == "true"
+ push.settings.store = store_status == "true"
+
+ if (! hub_status || hub_status == "true") {
+ push.subscribe("hub")
+ }
+ if (! store_status || store_status == "true") {
+ push.subscribe("store")
+ }
+ }
+ push.got_registration = function(data){
+ var registrationId = data.registrationId
+ var oldRegistrationId = localStorage.getItem("yoox.registrationId")
+ // console.log(registrationId, oldRegistrationId)
+
+ if (registrationId !== oldRegistrationId || ! push.settings.requested) {
+ localStorage.setItem("yoox.registrationId", registrationId)
+ push.subscribe("hub", function(){
+ push.subscribe("store")
+ })
+ }
+ }
+ push.subscribe = function(channel, cb){
+ if (push.disabled) return
+ push.settings[channel] = true
+ localStorage.setItem("yoox.push_" + channel, "true")
+ var data = {
+ registrationId: localStorage.getItem("yoox.registrationId"),
+ channel: channel,
+ platform: device.platform,
+ }
+ pushPlugin.subscribe(channel, function(){
+ console.log("subscribed to", channel)
+ })
+ $.ajax({
+ method: "POST",
+ url: push.url('add'),
+ data: data,
+ contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
+ success: function(){
+ console.log("subscribed to", channel)
+ cb && cb()
+ },
+ error: push.error,
+ })
+ }
+ push.unsubscribe = function(channel, cb){
+ if (push.disabled) return
+ push.settings[channel] = false
+ localStorage.setItem("yoox.push_" + channel, "false")
+ var data = {
+ registrationId: localStorage.getItem("yoox.registrationId"),
+ channel: channel,
+ platform: device.platform,
+ }
+ $.ajax({
+ method: "POST",
+ url: push.url('remove'),
+ data: data,
+ contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
+ success: function(){
+ console.log("unsubscribed from", channel)
+ cb && cb()
+ },
+ error: push.error,
+ })
+ }
+ push.url = function(key){
+ return sdk.cms() + '/_services/push/' + key
+ }
+ push.got_push_notification = function(push_obj) {
+ // console.log('We received this push notification: ' + JSON.stringify(push_obj));
+
+ app.blog.refresh()
+
+ push_obj.additionalData = push_obj.additionalData || {}
+
+ var is_hub = true
+
+ try {
+ is_hub = JSON.stringify(push_obj || {}).match(/hub/i)
+ }
+ catch (e) {
+ }
+
+ if (is_hub) {
+ app.intro.$alert.show().html("[ HUB UPDATED ]")
+ }
+ else if (! push_obj.additionalData.url) {
+ auth.clear_cart()
+ app.intro.$alert.show().html("[ STORE UPDATED ]")
+ }
+
+ if (push_obj.additionalData.foreground === false) {
+ // TODO: route the user to the uri in push_obj
+ pushPlugin.finish(function(){}, function(){})
+
+ if (push_obj.additionalData.url) {
+ app.deepLinkRoute = push_obj.additionalData.url
+ app.router.go(push_obj.additionalData.url)
+ }
+ }
+ else if (is_hub) {
+ app.router.go("hub")
+ }
+ else {
+ app.router.go("intro")
+ }
+ }
+ push.error = function(e){
+ console.log("push error")
+ }
+ return push
+})() \ No newline at end of file