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 } console.log("push init") // Register handlers FirebasePlugin.onMessageReceived(function(message) { try { console.log("onMessageReceived") console.dir(message) if (message.messageType === "notification") { push.got_push_notification(message) } } catch(e) { console.error("Exception in onMessageReceived callback: " + e.message) } }, function(error) { console.error("Failed receiving FirebasePlugin message", error); }) checkNotificationPermission(false) // Check permission then get token FirebasePlugin.setBadgeNumber(0) } var checkNotificationPermission = function(requested) { FirebasePlugin.hasPermission(function(hasPermission) { if (hasPermission) { // Granted console.log("Remote notifications permission granted") push.disabled = false getAPNSToken() } else if (!requested) { // Request permission console.log("Requesting remote notifications permission") FirebasePlugin.grantPermission(checkNotificationPermission.bind(this, true)) } else { // Denied push.disabled = true console.error("Notifications won't be shown as permission is denied") } }) } var getAPNSToken = function() { FirebasePlugin.getAPNSToken(function(token) { console.log("Got APNS token: " + token) if (!token) { console.log("Token is null, probably in simulator") return } push.did_initialize() push.got_registration(token) }, function(error) { console.error("Failed to get APNS token", error) }) } push.did_initialize = function() { 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" } push.got_registration = function(registrationId){ var oldRegistrationId = localStorage.getItem("yoox.registrationId") if (registrationId !== oldRegistrationId || ! push.settings.requested) { localStorage.setItem("yoox.push_requested", "true") localStorage.setItem("yoox.registrationId", registrationId) push.settings.requested = true push.subscribe("hub") 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, } FirebasePlugin.subscribe(channel, function(){ console.log("Subscribed to topic") }, function(error){ console.error("Error subscribing to topic: " + error) }) $.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, } FirebasePlugin.unsubscribe(channel, function(){ console.log("Unsubscribed from topic"); }, function(error){ console.error("Error unsubscribing from topic: " + error); }) $.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(message) { // console.log('We received this push notification: ' + JSON.stringify(push_obj)); app.blog.refresh() var is_hub = true FirebasePlugin.setBadgeNumber(0) try { is_hub = !! JSON.stringify(push_obj || {}).match(/hub/i) } catch (e) { } if (is_hub) { app.intro.$alert.show().html("[ HUB UPDATED ]") analytics.sendPageView("push/open/hub") app.router.go("intro") } else { auth.clear_cart() app.intro.$alert.show().html("[ STORE UPDATED ]") analytics.sendPageView("push/open/store") app.router.go("intro") } } push.error = function(e){ console.log("push error") } return push })()