diff --git a/app/core/AppConstants.js b/app/core/AppConstants.js index c309ace91af..8935a374fa9 100644 --- a/app/core/AppConstants.js +++ b/app/core/AppConstants.js @@ -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 }; diff --git a/app/core/TransactionsNotificationManager.js b/app/core/TransactionsNotificationManager.js index 07dad9cb980..a04336212c6 100644 --- a/app/core/TransactionsNotificationManager.js +++ b/app/core/TransactionsNotificationManager.js @@ -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 @@ -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 => { + 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 @@ -173,7 +210,7 @@ class TransactionsNotificationManager { Platform.OS === 'ios' && setTimeout(() => { - PushNotification.requestPermissions(); + this.requestPushNotificationsPermission(); }, 7000); }, 500); }); @@ -271,5 +308,8 @@ export default { }, gotIncomingTransaction(lastBlock) { return instance.gotIncomingTransaction(lastBlock); + }, + requestPushNotificationsPermission() { + return instance.requestPushNotificationsPermission(); } }; diff --git a/locales/en.json b/locales/en.json index 033c9703981..9b0f6f53388 100644 --- a/locales/en.json +++ b/locales/en.json @@ -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", diff --git a/locales/es.json b/locales/es.json index fe55684011a..8c39be5350a 100644 --- a/locales/es.json +++ b/locales/es.json @@ -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",