FIX: unsubscribe from push notifications when wallet is deleted

This commit is contained in:
Overtorment 2020-07-30 13:22:06 +01:00
parent 5fc7ec04ea
commit 1b00594c35
2 changed files with 34 additions and 1 deletions

View file

@ -173,6 +173,36 @@ const majorTomToGroundControl = async function (addresses, hashes, txids) {
);
};
/**
* The opposite of `majorTomToGroundControl` call.
*
* @param addresses {string[]}
* @param hashes {string[]}
* @param txids {string[]}
* @returns {Promise<object>} Response object from API rest call
*/
const unsubscribe = async function (addresses, hashes, txids) {
if (!Array.isArray(addresses) || !Array.isArray(hashes) || !Array.isArray(txids))
throw new Error('no addresses or hashes or txids provided');
const pushToken = await _getPushToken();
if (!pushToken || !pushToken.token || !pushToken.os) return;
const api = new Frisbee({ baseURI: constants.groundControlUri });
return await api.post(
'/unsubscribe',
Object.assign({}, _getHeaders(), {
body: {
addresses,
hashes,
txids,
token: pushToken.token,
os: pushToken.os,
},
}),
);
};
// on app launch (load module):
(async () => {
if (!(await _getPushToken())) return;
@ -186,3 +216,4 @@ PushNotification.setApplicationIconBadgeNumber(0);
module.exports.tryToObtainPermissions = tryToObtainPermissions;
module.exports.majorTomToGroundControl = majorTomToGroundControl;
module.exports.unsubscribe = unsubscribe;

View file

@ -30,8 +30,8 @@ import loc from '../../loc';
import { BlueCurrentTheme } from '../../components/themes';
const EV = require('../../blue_modules/events');
const prompt = require('../../blue_modules/prompt');
/** @type {AppStorage} */
const BlueApp = require('../../BlueApp');
const notifications = require('../../blue_modules/notifications');
const styles = StyleSheet.create({
root: {
@ -149,6 +149,7 @@ export default class WalletDetails extends Component {
if (Number(walletBalanceConfirmation) === this.state.wallet.getBalance()) {
this.props.navigation.setParams({ isLoading: true });
this.setState({ isLoading: true }, async () => {
notifications.unsubscribe(this.state.wallet.getAllExternalAddresses(), [], []);
BlueApp.deleteWallet(this.state.wallet);
ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false });
await BlueApp.saveToDisk();
@ -356,6 +357,7 @@ export default class WalletDetails extends Component {
} else {
this.props.navigation.setParams({ isLoading: true });
this.setState({ isLoading: true }, async () => {
notifications.unsubscribe(this.state.wallet.getAllExternalAddresses(), [], []);
BlueApp.deleteWallet(this.state.wallet);
ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false });
await BlueApp.saveToDisk();