summaryrefslogtreecommitdiff
path: root/StoneIsland/platforms/ios/www/js/lib/etc/push.js
blob: 9260ca1434b24952d33947315650a619eacf59ec (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
var push = (function(){
  var appId
  var clientKey
  var push = { settings: {} }
  push.init = function(){
    parsePlugin.initialize(appId, clientKey, push.did_initialize, push.error)
  }
  push.did_initialize = function() {
    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_" + channel, "true")
      push.subscribe("hub", function(){
        push.subscribe("store")
      })
    }
  }
  push.subscribe = function(channel, cb){
    parsePlugin.subscribe(channel, function(){
      push.settings[channel] = true
      localStorage.setItem("yoox.push_" + channel, "true")
      cb && cb()
    }, push.error)
  }
  push.unsubscribe = function(channel, cb){
    parsePlugin.unsubscribe(channel, function(){
      push.settings[channel] = false
      localStorage.setItem("yoox.push_" + channel, "false")
      cb && cb()
    }, push.error)
  }
  push.did_subscribe = function(){
    // parsePlugin.getInstallationId(function(id) {
    //   var install_data = {
    //     installation_id: id,
    //     channels: ['SampleChannel']
    //   }
    // }, push.error)
  }
  push.did_unsubscribe = function(){
    // 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.collection.loaded = false
    app.hub.loaded = false
    if (push_obj.receivedInForeground === false) {
      // TODO: route the user to the uri in push_obj
    }
    else {
      app.route("intro")
      try {
        var is_hub = JSON.stringify(push_obj || {}).match(/hub/i)
        if (is_hub) {
          app.intro.$alert.show().html("[ HUB UPDATED ]")
        }
        else {
          app.intro.$alert.show().html("[ STORE UPDATED ]")
        }
      catch (e) {
        app.intro.$alert.show().html("[ HUB UPDATED ]")
      }
    }
  }
  push.error = function(e){
    console.log("push error")
  }  
  return push
})()