summaryrefslogtreecommitdiff
path: root/StoneIsland/www/js/lib/etc/push.js
blob: ccdc39f5720a2ba5ec95bf485741020bc1dbe07d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var push = (function(){
  var appId = "GS82ZxpN8Mecpc53rsyu6aLLGK0W4CKi42J25DLB"
  var clientKey = "hQRtQfsgimYnX5PMivtcdXCG9eZhESeyTr0Rd8Sv"
  var push = { settings: {} }
  var pushPlugin
  push.init = function(){
  
    pushPlugin = PushNotification.init({
      ios: {
        alert: true,
        badge: true,
        sound: false,
        clearBadge: true,
      },
    })
    
    PushNotification.hasPermission(push.did_initialize)
    
    pushPlugin.on('registration', push.got_registration)
    
    pushPlugin.on('notification', push.got_push_notification)
  }
  
  push.got_registration = function(data){
    var registrationId = data.registrationId
    var oldRegistrationId =  localStorage.getItem("yoox.registrationId")
    console.log(registrationId, oldRegistrationId)
    
    if (registrationId !== oldRegistrationId) {
      localStorage.setItem("yoox.registrationId", registrationId)
      push.subscribe("hub", function(){
        push.subscribe("store")
      })
    }
  }
  push.did_initialize = function(data) {
    if (! data.isEnabled) {
      return
    }
    
    parsePlugin.registerCallback('onNotification', function(){
      window.onNotification = push.got_push_notification
    }, push.error)
    
    push.settings.requested = localStorage.getItem("yoox.push_requested") == "true"
    push.settings.hub = localStorage.getItem("yoox.push_hub") == "true"
    push.settings.store = localStorage.getItem("yoox.push_store") == "true"
    
    if ( ! push.settings.requested ) {
      localStorage.setItem("yoox.push_requested", "true")
      push.subscribe("hub", function(){
        push.subscribe("store")
      })
    }
  }
  push.subscribe = function(channel, cb){
    push.settings[channel] = true
    localStorage.setItem("yoox.push_" + channel, "true")
    var data = {
      registrationId: localStorage.getItem("yoox.registrationId"),
      channel: channel,
      platform: device.platform,
    }
    $.ajax({
      method: "POST",
      url: "https://stone.sup.land/_services/push/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){
    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: "https://stone.sup.land/_services/push/remove",
      data: JSON.stringify(),
      contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
      success: function(){
        console.log("unsubscribed from", channel)
        cb && cb()
      },
      error: push.error,
    })
  }
  // parsePlugin.getInstallationId(function(id) {
  //   var install_data = {
  //     installation_id: id,
  //     channels: ['SampleChannel']
  //   }
  // }, push.error)
  push.got_push_notification = function(push_obj) {
    // alert('We received this push notification: ' + JSON.stringify(push_obj));
    app.blog.refresh()

    try {
      var is_hub = JSON.stringify(push_obj || {}).match(/hub/i)
      if (is_hub) {
        app.intro.$alert.show().html("[ HUB UPDATED ]")
      }
      else {
        auth.clear_cart()
        app.intro.$alert.show().html("[ STORE UPDATED ]")
      }
    }
    catch (e) {
      app.intro.$alert.show().html("[ HUB UPDATED ]")
    }

    if (push_obj.receivedInForeground === false) {
      // TODO: route the user to the uri in push_obj
    }
    else {
      app.route("intro")
    }
  }
  push.error = function(e){
    console.log("push error")
  }  
  return push
})()