Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two step push notification prompt #589

Merged
merged 6 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/core/AppConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export default {
TX_CHECK_NORMAL_FREQUENCY: 10000,
TX_CHECK_BACKGROUND_FREQUENCY: 30000,
IPFS_OVERRIDE_PARAM: 'mm_override',
supportedTLDs: ['eth', 'xyz', 'test']
supportedTLDs: ['eth', 'xyz', 'test'],
MAX_PUSH_NOTIFICATION_PROMPT_TIMES: 2
};
44 changes: 42 additions & 2 deletions app/core/TransactionsNotificationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import Networks, { isKnownNetwork } from '../util/networks';
import { toChecksumAddress } from 'ethereumjs-util';
import { hexToBN, renderFromWei } from '../util/number';
import { strings } from '../../locales/i18n';
import { AppState, Platform } from 'react-native';
import { Alert, AppState, Platform } from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';
import AppConstants from './AppConstants';

/**
* Singleton class responsible for managing all the transaction
Expand Down Expand Up @@ -106,6 +108,41 @@ class TransactionsNotificationManager {
return TransactionsNotificationManager.instance;
}

/**
* Handles the push notification prompt
* with a custom set of rules, like max. number of attempts
*/
requestPushNotificationsPermission = async () => {
const promptCount = await AsyncStorage.getItem('@MetaMask:pushNotificationsPromptCount');
if (!promptCount || Number(promptCount) < AppConstants.MAX_PUSH_NOTIFICATION_PROMPT_TIMES) {
PushNotification.checkPermissions(permissions => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't use async await here because it's a native call made by the library 😬

if (!permissions || !permissions.alert) {
Alert.alert(
strings('notifications.prompt_title'),
strings('notifications.prompt_desc'),
[
{
text: strings('notifications.prompt_cancel'),
onPress: () => false,
style: 'default'
},
{
text: strings('notifications.prompt_ok'),
onPress: () => PushNotification.requestPermissions()
}
],
{ cancelable: false }
);

const times = (promptCount && Number(promptCount) + 1) || 1;
AsyncStorage.setItem('@MetaMask:pushNotificationsPromptCount', times.toString());
// In case we want to prompt again after certain time.
AsyncStorage.setItem('@MetaMask:pushNotificationsPromptTime', Date.now().toString());
}
});
}
};

/**
* Returns the id of the transaction that should
* be displayed and removes it from memory
Expand Down Expand Up @@ -173,7 +210,7 @@ class TransactionsNotificationManager {

Platform.OS === 'ios' &&
setTimeout(() => {
PushNotification.requestPermissions();
this.requestPushNotificationsPermission();
}, 7000);
}, 500);
});
Expand Down Expand Up @@ -271,5 +308,8 @@ export default {
},
gotIncomingTransaction(lastBlock) {
return instance.gotIncomingTransaction(lastBlock);
},
requestPushNotificationsPermission() {
return instance.requestPushNotificationsPermission();
}
};
6 changes: 5 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,11 @@
"pending_message": "Waiting for confirmation...",
"error_message": "Tap to view this transaction",
"success_message": "Tap to view this transaction",
"received_message": "Tap to view this transaction"
"received_message": "Tap to view this transaction",
"prompt_title": "Enable Push Notifications",
"prompt_desc": "Enable notifications so MetaMask can let you know when you've received ETH or when your transactions have been confirmed.",
"prompt_ok": "Yes",
"prompt_cancel": "No, thanks"
},
"secure_your_wallet_modal": {
"title": "Secure your wallet",
Expand Down
6 changes: 5 additions & 1 deletion locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,11 @@
"pending_message": "Tu transacción esta en proceso",
"error_message": "Presiona para ver esta transacción",
"success_message": "Presiona para ver esta transacción",
"received_message": "Presiona para ver esta transacción"
"received_message": "Presiona para ver esta transacción",
"prompt_title": "Habilitar Notificationes",
"prompt_desc": "Habilita las notificationes asi MetaMask puede avisarte cuando recibes ETH o cuando tus transacciones fueron confirmadas.",
"prompt_ok": "Si",
"prompt_cancel": "No, gracias"
},
"secure_your_wallet_modal": {
"title": "Protege tu billetera",
Expand Down