Skip to content

Commit bf4da9b

Browse files
maratalclaude
andcommitted
fix: sync Next.js push guide with tutorial app layout changes
- Add 'use client' to page.tsx snippet - Lift device ID display above the log area in PushApp - Pass onDeviceChange callback through PushActivationBanner - Move Clear button below the log in NotificationLog - Reduce flex gap from 20px to 10px Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 68a7a60 commit bf4da9b

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

src/pages/docs/push/getting-started/nextjs.mdx

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ Create `src/app/push/page.tsx` as the entry point:
6666

6767
<Code>
6868
```javascript
69+
'use client';
70+
6971
import dynamic from 'next/dynamic';
7072

7173
const PushApp = dynamic(() => import('./PushApp'), { ssr: false });
@@ -101,6 +103,7 @@ const client = new Ably.Realtime({
101103

102104
export default function PushApp() {
103105
const [output, setOutput] = useState<string[]>([]);
106+
const [deviceId, setDeviceId] = useState<string | null>(null);
104107

105108
function log(message: string) {
106109
setOutput((prev) => [...prev, message]);
@@ -110,15 +113,18 @@ export default function PushApp() {
110113
<AblyProvider client={client}>
111114
<main style={{ fontFamily: 'Arial, sans-serif', padding: '20px', maxWidth: '900px', margin: '0 auto' }}>
112115
<h1 style={{ marginBottom: '20px' }}>Ably Push Notifications — Next.js</h1>
113-
<div style={{ display: 'flex', gap: '20px' }}>
116+
<div style={{ display: 'flex', gap: '10px' }}>
114117
<div style={{ flex: '0 0 250px', padding: '15px', background: '#f0f0f0', borderRadius: '5px', height: 'fit-content' }}>
115118
<h3 style={{ marginTop: 0, marginBottom: '16px' }}>Push Notifications</h3>
116-
<PushActivationBanner onLog={log} />
119+
<PushActivationBanner onLog={log} onDeviceChange={setDeviceId} />
117120
<ChannelProvider channelName={CHANNEL_NAME}>
118121
<ChannelSubscription onLog={log} />
119122
</ChannelProvider>
120123
</div>
121-
<NotificationLog output={output} onClear={() => setOutput([])} />
124+
<div style={{ flex: 1 }}>
125+
{deviceId && <div style={{ padding: '10px 12px', marginBottom: '10px', background: '#f0f0f0', borderRadius: '5px', fontSize: '13px', color: '#555' }}>Device ID: {deviceId}</div>}
126+
<NotificationLog output={output} onClear={() => setOutput([])} />
127+
</div>
122128
</div>
123129
</main>
124130
</AblyProvider>
@@ -144,11 +150,16 @@ Create `src/app/push/PushActivationBanner.tsx`. This component uses the `usePush
144150
```javascript
145151
'use client';
146152

153+
import { useEffect } from 'react';
147154
import { usePushActivation } from 'ably/react';
148155

149-
export function PushActivationBanner({ onLog }: { onLog: (msg: string) => void }) {
156+
export function PushActivationBanner({ onLog, onDeviceChange }: { onLog: (msg: string) => void; onDeviceChange: (id: string | null) => void }) {
150157
const { activate, deactivate, localDevice } = usePushActivation();
151158

159+
useEffect(() => {
160+
onDeviceChange(localDevice?.id ?? null);
161+
}, [localDevice]);
162+
152163
async function handleActivate() {
153164
try {
154165
await activate();
@@ -171,7 +182,6 @@ export function PushActivationBanner({ onLog }: { onLog: (msg: string) => void }
171182

172183
return (
173184
<>
174-
{localDevice && <p style={{ margin: '0 0 8px' }}>Device ID: {localDevice.id}</p>}
175185
<button onClick={handleActivate} style={{ ...buttonStyle, background: '#28a745' }}>Activate Push</button>
176186
<button onClick={handleDeactivate} style={{ ...buttonStyle, background: '#dc3545' }}>Deactivate Push</button>
177187
</>
@@ -184,7 +194,7 @@ export function PushActivationBanner({ onLog }: { onLog: (msg: string) => void }
184194
185195
- **`activate`**: Registers the browser for push notifications. Requests notification permission, registers the service worker, and records the device with Ably.
186196
- **`deactivate`**: Removes the device registration from Ably's servers. Call this only on explicit user opt-out.
187-
- **`localDevice`**: The current `LocalDevice` if activated, `null` otherwise. Reactive — updates immediately when `activate` or `deactivate` is called, and is re-populated from `localStorage` on page load if the device was activated in a prior session.
197+
- **`localDevice`**: The current `LocalDevice` if activated, `null` otherwise. Reactive — updates immediately when `activate` or `deactivate` is called, and is re-populated from `localStorage` on page load if the device was activated in a prior session. Here it is propagated upward via `onDeviceChange` so the parent can display the device ID above the log.
188198
189199
## Step 3: Receive push notifications <a id="step-3"/>
190200
@@ -307,14 +317,14 @@ export function NotificationLog({ output, onClear }: { output: string[]; onClear
307317
const buttonStyle = { padding: '12px 20px', margin: '5px 0', width: '100%', display: 'block', background: '#6c757d', color: '#fff', border: 'none', borderRadius: '4px', cursor: 'pointer', fontSize: '14px' };
308318

309319
return (
310-
<div style={{ flex: 1 }}>
311-
<button onClick={onClear} style={buttonStyle}>Clear</button>
312-
<div style={{ border: '1px solid #ddd', borderRadius: '4px', padding: '12px', minHeight: '500px', background: '#fff', marginTop: '10px' }}>
320+
<>
321+
<div style={{ border: '1px solid #ddd', borderRadius: '4px', padding: '12px', minHeight: '500px', background: '#fff' }}>
313322
{output.map((entry, i) => (
314323
<p key={i} style={{ margin: '4px 0', borderLeft: '3px solid #007bff', paddingLeft: '8px', color: '#171717', whiteSpace: 'pre-line' }}>{entry}</p>
315324
))}
316325
</div>
317-
</div>
326+
<button onClick={onClear} style={buttonStyle}>Clear</button>
327+
</>
318328
);
319329
}
320330
```
@@ -442,7 +452,6 @@ export function ChannelSubscription({ onLog }: { onLog: (msg: string) => void })
442452
<>
443453
<button onClick={handleSubscribe} disabled={!isActivated} style={{ ...buttonStyle, background: '#6f42c1' }}>Subscribe to Channel</button>
444454
<button onClick={handleUnsubscribe} disabled={!isActivated} style={{ ...buttonStyle, background: '#fd7e14' }}>Unsubscribe from Channel</button>
445-
{!isActivated && <p style={{ fontSize: '12px', color: '#666', margin: '4px 0' }}>Activate push notifications first.</p>}
446455
</>
447456
);
448457
}

0 commit comments

Comments
 (0)