Skip to content

Commit

Permalink
notifs: Allow token-registration manual retry from troubleshooting sc…
Browse files Browse the repository at this point in the history
…reen
  • Loading branch information
chrisbobbe committed Apr 18, 2023
1 parent b8d87c5 commit 90c2d3a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
56 changes: 50 additions & 6 deletions src/settings/NotifTroubleshootingScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { RouteProp } from '../react-navigation';
import type { AppNavigationProp } from '../nav/AppNavigator';
import Screen from '../common/Screen';
import { createStyleSheet } from '../styles';
import { useGlobalSelector, useSelector } from '../react-redux';
import { useDispatch, useGlobalSelector, useSelector } from '../react-redux';
import {
getAccounts,
getGlobalSession,
Expand All @@ -29,7 +29,7 @@ import {
getZulipFeatureLevel,
tryGetActiveAccountState,
} from '../account/accountsSelectors';
import { showToast } from '../utils/info';
import { showErrorAlert, showToast } from '../utils/info';
import Input from '../common/Input';
import ZulipButton from '../common/ZulipButton';
import { identityOfAccount, keyOfIdentity } from '../account/accountMisc';
Expand All @@ -46,6 +46,8 @@ import isAppOwnDomain from '../isAppOwnDomain';
import { openLinkWithUserPreference, openSystemNotificationSettings } from '../utils/openLink';
import { getOwnUserRole, roleIsAtLeast } from '../permissionSelectors';
import { Role } from '../api/permissionsTypes';
import { initNotifications } from '../notification/notifTokens';
import { ApiError } from '../api/apiErrors';

const {
Notifications, // android
Expand Down Expand Up @@ -305,6 +307,7 @@ function useMailComposerIsAvailable(): boolean | null {
*/
export default function NotifTroubleshootingScreen(props: Props): React.Node {
const _ = React.useContext(TranslationContext);
const dispatch = useDispatch();

const settings = useGlobalSelector(getGlobalSettings);

Expand Down Expand Up @@ -339,6 +342,28 @@ export default function NotifTroubleshootingScreen(props: Props): React.Node {
[],
);

const handlePressRetryRegister = React.useCallback(async () => {
try {
await dispatch(initNotifications());
} catch (errorIllTyped) {
const error: mixed = errorIllTyped; // https://github.com/facebook/flow/issues/2470

if (!(error instanceof Error)) {
logging.error('Unexpected non-error thrown');
}

let msg = undefined;
if (error instanceof ApiError) {
msg = _('The server said:\n\n{errorMessage}', {
errorMessage: error.message,
});
} else if (error instanceof Error && error.message.length > 0) {
msg = error.message;
}
showErrorAlert(_('Registration failed'), msg);
}
}, [_, dispatch]);

const reportJson = React.useMemo(() => jsonifyNotificationReport(report), [report]);

const handlePressCopy = React.useCallback(() => {
Expand Down Expand Up @@ -467,10 +492,29 @@ export default function NotifTroubleshootingScreen(props: Props): React.Node {
break;

case NotificationProblem.TokenNotAcked: {
// TODO: Could offer:
// - Re-request registering token with server, and show if a/the
// request hasn't completed
alerts.push(genericAlert);
invariant(
report.registerPushTokenRequestsInProgress != null,
'report.registerPushTokenRequestsInProgress missing for active account?',
);
const isInProgress = report.registerPushTokenRequestsInProgress > 0;
alerts.push(
<AlertItem
bottomMargin
text={{
text: isInProgress
? 'The Zulip server at {realm} has not yet registered your device token. A request is in progress.'
: 'The Zulip server at {realm} has not yet registered your device token.',
values: { realm: identity.realm.toString() },
}}
buttons={
isInProgress
? undefined
: [{ id: 'retry', label: 'Retry', onPress: handlePressRetryRegister }]
}
/>,
);

alerts.push(genericAlert); // Still point to support for good measure
break;
}

Expand Down
4 changes: 4 additions & 0 deletions static/translations/messages_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
"Remind me later": "Remind me later",
"The Zulip server at {realm} is not set up to deliver push notifications.": "The Zulip server at {realm} is not set up to deliver push notifications.",
"The Zulip server at {realm} is not set up to deliver push notifications. Please contact your administrator.": "The Zulip server at {realm} is not set up to deliver push notifications. Please contact your administrator.",
"The Zulip server at {realm} has not yet registered your device token. A request is in progress.": "The Zulip server at {realm} has not yet registered your device token. A request is in progress.",
"The Zulip server at {realm} has not yet registered your device token.": "The Zulip server at {realm} has not yet registered your device token.",
"Registration failed": "Registration failed",
"Retry": "Retry",
"Message not saved": "Message not saved",
"Save message": "Save message",
"Send message": "Send message",
Expand Down

0 comments on commit 90c2d3a

Please sign in to comment.