New push notifications
This commit is contained in:
parent
aeea866623
commit
4e8bd0634c
13 changed files with 139 additions and 153 deletions
|
|
@ -1,87 +1,123 @@
|
|||
'use strict';
|
||||
angular.module('copayApp.services')
|
||||
.factory('pushNotificationsService', function($log, platformInfo, storageService, configService, lodash) {
|
||||
var root = {};
|
||||
var isCordova = platformInfo.isCordova;
|
||||
var isWP = platformInfo.isWP;
|
||||
var isIOS = platformInfo.isIOS;
|
||||
var isAndroid = platformInfo.isAndroid;
|
||||
angular.module('copayApp.services').factory('pushNotificationsService', function pushNotificationsService($log, $state, $ionicHistory, platformInfo, lodash, appConfigService, profileService, configService) {
|
||||
var root = {};
|
||||
var isIOS = platformInfo.isIOS;
|
||||
var isAndroid = platformInfo.isAndroid;
|
||||
var usePushNotifications = platformInfo.isCordova && !platformInfo.isWP;
|
||||
|
||||
var usePushNotifications = isCordova && !isWP;
|
||||
var _token = null;
|
||||
|
||||
root.init = function(walletsClients) {
|
||||
var defaults = configService.getDefaults();
|
||||
try {
|
||||
var push = PushNotification.init(defaults.pushNotifications.config);
|
||||
} catch(e) {
|
||||
$log.error(e);
|
||||
return;
|
||||
};
|
||||
root.init = function() {
|
||||
if (!usePushNotifications || _token) return;
|
||||
configService.whenAvailable(function(config) {
|
||||
if (!config.pushNotificationsEnabled) return;
|
||||
|
||||
$log.debug('Starting push notification registration...');
|
||||
|
||||
push.on('registration', function(data) {
|
||||
$log.debug('Starting push notification registration');
|
||||
root.token = data.registrationId;
|
||||
var config = configService.getSync();
|
||||
if (config.pushNotifications.enabled) root.enableNotifications(walletsClients);
|
||||
});
|
||||
//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();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
return push;
|
||||
root.enable = function() {
|
||||
if (!_token) {
|
||||
$log.warn('No token available for this device. Cannot set push notifications. Needs registration.');
|
||||
return;
|
||||
}
|
||||
|
||||
root.enableNotifications = function(walletsClients) {
|
||||
if (!usePushNotifications) return;
|
||||
var wallets = profileService.getWallets();
|
||||
lodash.forEach(wallets, function(walletClient) {
|
||||
_subscribe(walletClient);
|
||||
});
|
||||
};
|
||||
|
||||
var config = configService.getSync();
|
||||
if (!config.pushNotifications.enabled) return;
|
||||
root.disable = function() {
|
||||
if (!_token) {
|
||||
$log.warn('No token available for this device. Cannot disable push notifications.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!root.token) {
|
||||
$log.warn('No token available for this device. Cannot set push notifications. Needs registration.');
|
||||
return;
|
||||
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.');
|
||||
});
|
||||
};
|
||||
|
||||
var _openWallet = function(walletId) {
|
||||
var wallet = profileService.getWallet(walletId);
|
||||
if (!wallet) return;
|
||||
if (!wallet.isComplete()) {
|
||||
return $state.go('tabs.copayers', {
|
||||
walletId: walletId
|
||||
});
|
||||
}
|
||||
|
||||
$state.go('tabs.wallet', {
|
||||
walletId: walletId
|
||||
});
|
||||
};
|
||||
|
||||
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));
|
||||
var walletId, copayerId;
|
||||
if(data.wasTapped) {
|
||||
// Notification was received on device tray and tapped by the user.
|
||||
var walletId = data ? data.walletId : null;
|
||||
if (!walletId) return;
|
||||
$ionicHistory.nextViewOptions({
|
||||
disableAnimate: true,
|
||||
historyRoot: true
|
||||
});
|
||||
$ionicHistory.clearHistory();
|
||||
$state.go('tabs.home').then(function() {
|
||||
_openWallet(walletId);
|
||||
});
|
||||
} else {
|
||||
// TODO
|
||||
// Notification was received in foreground. Maybe the user needs to be notified.
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
lodash.forEach(walletsClients, function(walletClient) {
|
||||
var opts = {};
|
||||
opts.type = isIOS ? "ios" : isAndroid ? "android" : null;
|
||||
opts.token = root.token;
|
||||
root.subscribe(opts, walletClient, function(err, response) {
|
||||
if (err) $log.warn('Subscription error: ' + err.message + ': ' + JSON.stringify(opts));
|
||||
else $log.debug('Subscribed to push notifications service: ' + JSON.stringify(response));
|
||||
});
|
||||
});
|
||||
}
|
||||
return root;
|
||||
|
||||
root.disableNotifications = function(walletsClients) {
|
||||
if (!usePushNotifications) return;
|
||||
|
||||
lodash.forEach(walletsClients, function(walletClient) {
|
||||
root.unsubscribe(walletClient, function(err) {
|
||||
if (err) $log.warn('Unsubscription error: ' + err.message);
|
||||
else $log.debug('Unsubscribed from push notifications service');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
root.subscribe = function(opts, walletClient, cb) {
|
||||
if (!usePushNotifications) return cb();
|
||||
|
||||
var config = configService.getSync();
|
||||
if (!config.pushNotifications.enabled) return;
|
||||
|
||||
walletClient.pushNotificationsSubscribe(opts, function(err, resp) {
|
||||
if (err) return cb(err);
|
||||
return cb(null, resp);
|
||||
});
|
||||
}
|
||||
|
||||
root.unsubscribe = function(walletClient, cb) {
|
||||
if (!usePushNotifications) return cb();
|
||||
|
||||
walletClient.pushNotificationsUnsubscribe(function(err) {
|
||||
if (err) return cb(err);
|
||||
return cb(null);
|
||||
});
|
||||
}
|
||||
|
||||
return root;
|
||||
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue