summaryrefslogtreecommitdiff
path: root/StoneIsland/platforms/ios/www/js/lib/etc/push.js
diff options
context:
space:
mode:
Diffstat (limited to 'StoneIsland/platforms/ios/www/js/lib/etc/push.js')
-rwxr-xr-xStoneIsland/platforms/ios/www/js/lib/etc/push.js144
1 files changed, 79 insertions, 65 deletions
diff --git a/StoneIsland/platforms/ios/www/js/lib/etc/push.js b/StoneIsland/platforms/ios/www/js/lib/etc/push.js
index 74aaf7b5..1f0ab9b6 100755
--- a/StoneIsland/platforms/ios/www/js/lib/etc/push.js
+++ b/StoneIsland/platforms/ios/www/js/lib/etc/push.js
@@ -1,34 +1,68 @@
var push = (function(){
var push = { settings: {}, disabled: false }
- var pushPlugin
+ // 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)
+
+ // 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)
}
- push.did_initialize = function(data) {
- // console.log(data)
- if (! data.isEnabled) {
- console.log("push did not initialize")
- return
- }
+
+ 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")
@@ -36,30 +70,19 @@ var push = (function(){
push.settings.requested = localStorage.getItem("yoox.push_requested") == "true"
push.settings.hub = hub_status == "true"
push.settings.store = store_status == "true"
-
- if (push.settings.requested) {
- return
- }
- // not sure why we're also signing up for notifications here??
- 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
+
+ push.got_registration = function(registrationId){
var oldRegistrationId = localStorage.getItem("yoox.registrationId")
- // console.log(registrationId, oldRegistrationId)
-
if (registrationId !== oldRegistrationId || ! push.settings.requested) {
+ localStorage.setItem("yoox.push_requested", "true")
localStorage.setItem("yoox.registrationId", registrationId)
- push.subscribe("hub", function(){
- push.subscribe("store")
- })
+ push.settings.requested = true
+ push.subscribe("hub")
+ push.subscribe("store")
}
}
+
push.subscribe = function(channel, cb){
if (push.disabled) return
push.settings[channel] = true
@@ -69,8 +92,10 @@ var push = (function(){
channel: channel,
platform: device.platform,
}
- pushPlugin.subscribe(channel, function(){
- console.log("subscribed to", channel)
+ FirebasePlugin.subscribe(channel, function(){
+ console.log("Subscribed to topic")
+ }, function(error){
+ console.error("Error subscribing to topic: " + error)
})
$.ajax({
method: "POST",
@@ -93,6 +118,11 @@ var push = (function(){
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'),
@@ -108,42 +138,26 @@ var push = (function(){
push.url = function(key){
return sdk.cms() + '/_services/push/' + key
}
- push.got_push_notification = function(push_obj) {
+ push.got_push_notification = function(message) {
// console.log('We received this push notification: ' + JSON.stringify(push_obj));
-
app.blog.refresh()
- push_obj.additionalData = push_obj.additionalData || {}
-
var is_hub = true
+ FirebasePlugin.setBadgeNumber(0)
+
try {
- is_hub = JSON.stringify(push_obj || {}).match(/hub/i)
- }
- catch (e) {
- }
+ 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) {
+ analytics.sendPageView("push/open/hub")
+ app.router.go("intro")
+ } else {
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 {
+ analytics.sendPageView("push/open/store")
app.router.go("intro")
}
}