diff options
| author | Jules Laplace <jules@okfoc.us> | 2017-02-16 01:24:12 +0100 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2017-02-16 01:24:12 +0100 |
| commit | 30c49550c89c1b69c680170d2dc247eac76bd463 (patch) | |
| tree | 8732652298b630b9ba15def97e59738f1c9bf7b6 /StoneIsland/www/js/lib/etc/push.js | |
| parent | 8f1f626384e6ba75f4fb24c27e0973260a74421b (diff) | |
push plugin
Diffstat (limited to 'StoneIsland/www/js/lib/etc/push.js')
| -rwxr-xr-x | StoneIsland/www/js/lib/etc/push.js | 86 |
1 files changed, 70 insertions, 16 deletions
diff --git a/StoneIsland/www/js/lib/etc/push.js b/StoneIsland/www/js/lib/etc/push.js index 634ffe6d..ccdc39f5 100755 --- a/StoneIsland/www/js/lib/etc/push.js +++ b/StoneIsland/www/js/lib/etc/push.js @@ -2,12 +2,42 @@ var push = (function(){ var appId = "GS82ZxpN8Mecpc53rsyu6aLLGK0W4CKi42J25DLB" var clientKey = "hQRtQfsgimYnX5PMivtcdXCG9eZhESeyTr0Rd8Sv" var push = { settings: {} } - var parsePlugin + var pushPlugin push.init = function(){ - parsePlugin = window.parsePlugin || { initialize: function(){} } - parsePlugin.initialize(appId, clientKey, push.did_initialize, push.error) + + 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.did_initialize = function() { + + 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) @@ -24,20 +54,44 @@ var push = (function(){ } } push.subscribe = function(channel, cb){ - parsePlugin.subscribe(channel, function(){ - push.settings[channel] = true - localStorage.setItem("yoox.push_" + channel, "true") - console.log("subscribed to", channel) - cb && cb() - }, push.error) + 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){ - parsePlugin.unsubscribe(channel, function(){ - push.settings[channel] = false - localStorage.setItem("yoox.push_" + channel, "false") - console.log("unsubscribed from", channel) - cb && cb() - }, push.error) + 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 = { |
