Compare commits
3 Commits
2603ca8e5c
...
0fb3da20b9
| Author | SHA1 | Date | |
|---|---|---|---|
| 0fb3da20b9 | |||
| e460dbd34e | |||
| 0de2fd942f |
@@ -170,22 +170,32 @@ type PushStatus = {
|
|||||||
registered: boolean;
|
registered: boolean;
|
||||||
distributor: string;
|
distributor: string;
|
||||||
endpoint: string;
|
endpoint: string;
|
||||||
|
distributors: string[];
|
||||||
|
lastFailure: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Android-only section showing UnifiedPush registration status and controls. */
|
/** Android-only section showing UnifiedPush registration status and controls. */
|
||||||
function AndroidPushNotifications() {
|
function AndroidPushNotifications() {
|
||||||
const [status, setStatus] = useState<PushStatus | undefined>(undefined);
|
const [status, setStatus] = useState<PushStatus | undefined>(undefined);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [lastError, setLastError] = useState<string | undefined>(undefined);
|
||||||
|
|
||||||
const refresh = useCallback(async () => {
|
const refresh = useCallback(async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
const s = await getBackgroundSyncStatus();
|
const s = await getBackgroundSyncStatus();
|
||||||
|
const distributors = Array.isArray(s?.distributors)
|
||||||
|
? s.distributors
|
||||||
|
: typeof s?.distributors === 'string' && s.distributors && s.distributors !== '[]'
|
||||||
|
? [s.distributors]
|
||||||
|
: [];
|
||||||
setStatus(
|
setStatus(
|
||||||
s
|
s
|
||||||
? {
|
? {
|
||||||
registered: s.registered,
|
registered: s.registered,
|
||||||
distributor: s.distributor || '',
|
distributor: s.distributor || '',
|
||||||
endpoint: s.endpoint || '',
|
endpoint: s.endpoint || '',
|
||||||
|
distributors,
|
||||||
|
lastFailure: s.lastFailure || '',
|
||||||
}
|
}
|
||||||
: undefined
|
: undefined
|
||||||
);
|
);
|
||||||
@@ -198,8 +208,14 @@ function AndroidPushNotifications() {
|
|||||||
|
|
||||||
const [resetState, reset] = useAsyncCallback(
|
const [resetState, reset] = useAsyncCallback(
|
||||||
useCallback(async () => {
|
useCallback(async () => {
|
||||||
await requestResetPushRegistration();
|
setLastError(undefined);
|
||||||
|
const result = await requestResetPushRegistration();
|
||||||
await refresh();
|
await refresh();
|
||||||
|
if (!result.success) {
|
||||||
|
setLastError(
|
||||||
|
'Selected distributor but did not get a push endpoint. In ntfy: enable UnifiedPush, allow unrestricted battery, then try Reset again.'
|
||||||
|
);
|
||||||
|
}
|
||||||
}, [refresh])
|
}, [refresh])
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -214,6 +230,43 @@ function AndroidPushNotifications() {
|
|||||||
resetState.status === AsyncStatus.Loading ||
|
resetState.status === AsyncStatus.Loading ||
|
||||||
pingState.status === AsyncStatus.Loading;
|
pingState.status === AsyncStatus.Loading;
|
||||||
|
|
||||||
|
const statusDescription = (() => {
|
||||||
|
if (loading) return 'Loading status…';
|
||||||
|
if (status === undefined) {
|
||||||
|
return (
|
||||||
|
<Text as="span" style={{ color: color.Critical.Main }} size="T200">
|
||||||
|
Failed to read push status.
|
||||||
|
</Text>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (status.registered) {
|
||||||
|
return (
|
||||||
|
<Text as="span" size="T200">
|
||||||
|
{`Distributor: ${status.distributor || 'unknown'}`}
|
||||||
|
</Text>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (status.lastFailure) {
|
||||||
|
return (
|
||||||
|
<Text as="span" style={{ color: color.Critical.Main }} size="T200">
|
||||||
|
{`Registration failed (${status.lastFailure}). Tap Reset and pick ntfy again.`}
|
||||||
|
</Text>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (status.distributors.length === 0) {
|
||||||
|
return (
|
||||||
|
<Text as="span" style={{ color: color.Critical.Main }} size="T200">
|
||||||
|
No UnifiedPush distributor found. Install ntfy from Play Store/F-Droid and enable UnifiedPush in ntfy settings.
|
||||||
|
</Text>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<Text as="span" style={{ color: color.Warning?.Main ?? color.Critical.Main }} size="T200">
|
||||||
|
{`Found ${status.distributors.join(', ')} but not registered yet. Tap Reset and pick ntfy.`}
|
||||||
|
</Text>
|
||||||
|
);
|
||||||
|
})();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box direction="Column" gap="100">
|
<Box direction="Column" gap="100">
|
||||||
<Text size="L400">Android Push (UnifiedPush)</Text>
|
<Text size="L400">Android Push (UnifiedPush)</Text>
|
||||||
@@ -226,29 +279,20 @@ function AndroidPushNotifications() {
|
|||||||
<SettingTile
|
<SettingTile
|
||||||
title="Background Notifications"
|
title="Background Notifications"
|
||||||
description={
|
description={
|
||||||
loading ? (
|
<>
|
||||||
'Loading status…'
|
{statusDescription}
|
||||||
) : status === undefined ? (
|
{lastError ? (
|
||||||
<Text as="span" style={{ color: color.Critical.Main }} size="T200">
|
<Text as="span" style={{ color: color.Critical.Main, display: 'block' }} size="T200">
|
||||||
Failed to read push status.
|
{lastError}
|
||||||
</Text>
|
|
||||||
) : status.registered ? (
|
|
||||||
<>
|
|
||||||
<Text as="span" size="T200">
|
|
||||||
{`Distributor: ${status.distributor || 'unknown'}`}
|
|
||||||
</Text>
|
</Text>
|
||||||
</>
|
) : null}
|
||||||
) : (
|
</>
|
||||||
<Text as="span" style={{ color: color.Warning?.Main ?? color.Critical.Main }} size="T200">
|
|
||||||
Not registered. Tap "Reset" to choose a distributor app.
|
|
||||||
</Text>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
after={loading ? <Spinner variant="Secondary" /> : undefined}
|
after={loading ? <Spinner variant="Secondary" /> : undefined}
|
||||||
/>
|
/>
|
||||||
<SettingTile
|
<SettingTile
|
||||||
title="Change Distributor"
|
title="Change Distributor"
|
||||||
description="Re-open the UnifiedPush distributor selection dialog."
|
description="Shows a list of installed UnifiedPush apps (ntfy, etc.) and registers the one you pick."
|
||||||
after={
|
after={
|
||||||
<Button
|
<Button
|
||||||
size="300"
|
size="300"
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ type UnifiedPushStatus = {
|
|||||||
registered: boolean;
|
registered: boolean;
|
||||||
distributor: string;
|
distributor: string;
|
||||||
distributors: string[] | string;
|
distributors: string[] | string;
|
||||||
|
lastFailure?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
type UnifiedPushEndpointEvent = {
|
type UnifiedPushEndpointEvent = {
|
||||||
@@ -55,6 +56,8 @@ interface MatrixBackgroundSyncPlugin {
|
|||||||
setAppForeground(options: { foreground: boolean }): Promise<void>;
|
setAppForeground(options: { foreground: boolean }): Promise<void>;
|
||||||
/** Returns fetch state and current UnifiedPush registration details. */
|
/** Returns fetch state and current UnifiedPush registration details. */
|
||||||
getStatus(): Promise<UnifiedPushStatus>;
|
getStatus(): Promise<UnifiedPushStatus>;
|
||||||
|
/** Native Matrix gateway discovery for a UnifiedPush endpoint (avoids WebView CORS). */
|
||||||
|
resolveUnifiedPushGateway(options: { endpoint: string }): Promise<{ gatewayUrl: string }>;
|
||||||
addListener(
|
addListener(
|
||||||
eventName: 'unifiedPushNewEndpoint',
|
eventName: 'unifiedPushNewEndpoint',
|
||||||
listenerFunc: (event: UnifiedPushEndpointEvent) => void
|
listenerFunc: (event: UnifiedPushEndpointEvent) => void
|
||||||
@@ -141,6 +144,17 @@ const normalizeDistributors = (raw: UnifiedPushStatus['distributors']): string[]
|
|||||||
};
|
};
|
||||||
|
|
||||||
const resolveUnifiedPushGateway = async (endpoint: string): Promise<string> => {
|
const resolveUnifiedPushGateway = async (endpoint: string): Promise<string> => {
|
||||||
|
// Prefer native HTTP — ntfy/Matrix gateway responses omit CORS headers, so
|
||||||
|
// WebView fetch fails with TypeError: Failed to fetch and wrongly falls back.
|
||||||
|
try {
|
||||||
|
const { registerPlugin } = await loadCapacitorCore();
|
||||||
|
const MatrixBackgroundSync = registerPlugin<MatrixBackgroundSyncPlugin>('MatrixBackgroundSync');
|
||||||
|
const result = await MatrixBackgroundSync.resolveUnifiedPushGateway({ endpoint });
|
||||||
|
if (result?.gatewayUrl) return result.gatewayUrl;
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('[BackgroundSync] Native UnifiedPush gateway discovery failed:', err);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const discoveryUrl = new URL(endpoint);
|
const discoveryUrl = new URL(endpoint);
|
||||||
discoveryUrl.pathname = '/_matrix/push/v1/notify';
|
discoveryUrl.pathname = '/_matrix/push/v1/notify';
|
||||||
@@ -529,7 +543,23 @@ export const requestResetPushRegistration = async (): Promise<{ success: boolean
|
|||||||
const MatrixBackgroundSync = registerPlugin<MatrixBackgroundSyncPlugin>('MatrixBackgroundSync');
|
const MatrixBackgroundSync = registerPlugin<MatrixBackgroundSyncPlugin>('MatrixBackgroundSync');
|
||||||
const result = await MatrixBackgroundSync.requestDistributorSetup();
|
const result = await MatrixBackgroundSync.requestDistributorSetup();
|
||||||
console.log('[BackgroundSync] requestDistributorSetup completed:', result);
|
console.log('[BackgroundSync] requestDistributorSetup completed:', result);
|
||||||
return result ?? { success: false };
|
if (!result?.success) return { success: false };
|
||||||
|
|
||||||
|
// Endpoint arrives asynchronously from the distributor after register().
|
||||||
|
for (let i = 0; i < 40; i += 1) {
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 250));
|
||||||
|
const status = await getBackgroundSyncStatus();
|
||||||
|
if (status?.registered && status.endpoint) {
|
||||||
|
return { success: true };
|
||||||
|
}
|
||||||
|
if (status?.lastFailure) {
|
||||||
|
console.warn('[BackgroundSync] Registration failed while waiting:', status.lastFailure);
|
||||||
|
return { success: false };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.warn('[BackgroundSync] Distributor selected but no endpoint received in time');
|
||||||
|
return { success: false };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('[BackgroundSync] requestDistributorSetup failed:', err);
|
console.error('[BackgroundSync] requestDistributorSetup failed:', err);
|
||||||
return { success: false };
|
return { success: false };
|
||||||
|
|||||||
Reference in New Issue
Block a user