Wallet/src/js/services/pushNotificationsService.js

130 lines
4 KiB
JavaScript
Raw Normal View History

2016-01-04 14:11:24 -03:00
'use strict';
2017-02-03 21:12:22 -03:00
angular.module('copayApp.services').factory('pushNotificationsService', function pushNotificationsService($log, $state, $ionicHistory, sjcl, platformInfo, lodash, appConfigService, profileService, configService) {
2017-02-03 18:03:29 -03:00
var root = {};
var isIOS = platformInfo.isIOS;
var isAndroid = platformInfo.isAndroid;
var usePushNotifications = platformInfo.isCordova && !platformInfo.isWP;
var _token = null;
root.init = function() {
if (!usePushNotifications || _token) return;
configService.whenAvailable(function(config) {
if (!config.pushNotificationsEnabled) return;
$log.debug('Starting push notification registration...');
//Keep in mind the function will return null if the token has not been established yet.
FCMPlugin.getToken(function(token) {
$log.debug('Get token for push notifications...');
_token = token;
root.enable();
});
});
};
root.enable = function() {
if (!_token) {
$log.warn('No token available for this device. Cannot set push notifications. Needs registration.');
return;
2016-01-04 14:11:24 -03:00
}
2017-02-03 18:03:29 -03:00
var wallets = profileService.getWallets();
lodash.forEach(wallets, function(walletClient) {
_subscribe(walletClient);
});
};
2016-01-20 16:06:15 -03:00
2017-02-03 18:03:29 -03:00
root.disable = function() {
if (!_token) {
$log.warn('No token available for this device. Cannot disable push notifications.');
return;
2016-01-04 14:11:24 -03:00
}
2017-02-03 18:03:29 -03:00
var wallets = profileService.getWallets();
lodash.forEach(wallets, function(walletClient) {
_unsubscribe(walletClient);
});
_token = null;
};
root.unsubscribe = function(walletClient) {
if (!_token) return;
_unsubscribe(walletClient);
};
var _subscribe = function(walletClient) {
var opts = {
token : _token,
platform: isIOS ? 'ios' : isAndroid ? 'android' : null,
packageName : appConfigService.packageNameId
};
walletClient.pushNotificationsSubscribe(opts, function(err) {
if (err) $log.error(walletClient.name + ': Subscription Push Notifications error. ', JSON.stringify(err));
else $log.debug(walletClient.name + ': Subscription Push Notifications success.');
});
};
var _unsubscribe = function(walletClient, cb) {
walletClient.pushNotificationsUnsubscribe(_token, function(err) {
if (err) $log.error(walletClient.name + ': Unsubscription Push Notifications error. ', JSON.stringify(err));
else $log.debug(walletClient.name + ': Unsubscription Push Notifications Success.');
});
};
2017-02-03 21:12:22 -03:00
var _openWallet = function(walletIdHashed) {
var wallets = profileService.getWallets();
var wallet = lodash.find(wallets, function(w) {
return (lodash.isEqual(walletIdHashed, sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(w.id))));
});
2017-02-03 18:03:29 -03:00
if (!wallet) return;
2017-02-03 21:12:22 -03:00
2017-02-03 18:03:29 -03:00
if (!wallet.isComplete()) {
return $state.go('tabs.copayers', {
2017-02-03 21:12:22 -03:00
walletId: wallet.id
2016-01-04 16:35:34 -03:00
});
}
2017-02-03 18:03:29 -03:00
$state.go('tabs.wallet', {
2017-02-03 21:12:22 -03:00
walletId: wallet.id
2017-02-03 18:03:29 -03:00
});
};
if (usePushNotifications) {
FCMPlugin.onTokenRefresh(function(token) {
if (!_token) return;
$log.debug('Refresh and update token for push notifications...');
_token = token;
root.enable();
});
FCMPlugin.onNotification(function(data) {
if (!_token) return;
$log.debug('New Event Push onNotification: ' + JSON.stringify(data));
if(data.wasTapped) {
// Notification was received on device tray and tapped by the user.
2017-02-03 21:12:22 -03:00
var walletIdHashed = data.walletId;
if (!walletIdHashed) return;
2017-02-03 18:03:29 -03:00
$ionicHistory.nextViewOptions({
disableAnimate: true,
historyRoot: true
});
$ionicHistory.clearHistory();
2017-03-09 14:43:17 -03:00
$state.go('tabs.home', {}, {
'reload': true,
'notify': $state.current.name == 'tabs.home' ? false : true
}).then(function() {
2017-02-03 21:12:22 -03:00
_openWallet(walletIdHashed);
2017-02-03 18:03:29 -03:00
});
} else {
// TODO
// Notification was received in foreground. Maybe the user needs to be notified.
}
});
}
2016-01-04 14:11:24 -03:00
2017-02-03 18:03:29 -03:00
return root;
2016-01-04 14:11:24 -03:00
2017-02-03 18:03:29 -03:00
});