Skip to content

Commit ebef656

Browse files
committed
Remove unsupported in flutter push admin methods. Cleanup button constructors.
1 parent 120fcb7 commit ebef656

1 file changed

Lines changed: 23 additions & 75 deletions

File tree

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

Lines changed: 23 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -385,30 +385,31 @@ Widget build(BuildContext context) {
385385
const SizedBox(height: 12),
386386
ElevatedButton(
387387
onPressed: _activatePush,
388-
style:
389-
ElevatedButton.styleFrom(backgroundColor: Colors.green),
390-
child: const Text('Activate Push',
391-
style: TextStyle(color: Colors.white)),
388+
style: ElevatedButton.styleFrom(
389+
backgroundColor: Colors.green,
390+
foregroundColor: Colors.white),
391+
child: const Text('Activate Push'),
392392
),
393393
ElevatedButton(
394394
onPressed: _deactivatePush,
395-
style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
396-
child: const Text('Deactivate Push',
397-
style: TextStyle(color: Colors.white)),
395+
style: ElevatedButton.styleFrom(
396+
backgroundColor: Colors.red,
397+
foregroundColor: Colors.white),
398+
child: const Text('Deactivate Push'),
398399
),
399400
ElevatedButton(
400401
onPressed: _subscribeToChannel,
401402
style: ElevatedButton.styleFrom(
402-
backgroundColor: Colors.purple),
403-
child: const Text('Subscribe to Channel',
404-
style: TextStyle(color: Colors.white)),
403+
backgroundColor: Colors.purple,
404+
foregroundColor: Colors.white),
405+
child: const Text('Subscribe to Channel'),
405406
),
406407
ElevatedButton(
407408
onPressed: _unsubscribeFromChannel,
408409
style: ElevatedButton.styleFrom(
409-
backgroundColor: Colors.orange),
410-
child: const Text('Unsubscribe from Channel',
411-
style: TextStyle(color: Colors.white)),
410+
backgroundColor: Colors.orange,
411+
foregroundColor: Colors.white),
412+
child: const Text('Unsubscribe from Channel'),
412413
),
413414
const SizedBox(height: 12),
414415
Expanded(
@@ -455,59 +456,17 @@ ably channels publish exampleChannel1 '{"name":"example","data":"Hello from CLI!
455456

456457
## Step 5: Send push with code <a id="step-5"/>
457458

458-
Just as you can send push notifications through the Ably CLI or dashboard, you can also send them directly from your app using `deviceId`, `clientId`, or channel publishing methods. Add the following methods to `_PushHomePageState`:
459+
The `ably_flutter` SDK does not implement the Push Admin API, so sending push directly to a `deviceId` or `clientId` from a Flutter client is not supported. Use the [Ably REST API](/docs/api/rest-sdk) from your backend server for direct push.
459460

460-
<Code>
461-
```flutter
462-
Future<void> _sendPushToDevice() async {
463-
try {
464-
final device = await AblyService().realtime.device();
465-
await AblyService().realtime.push.admin.publish(
466-
{'deviceId': device.id},
467-
{
468-
'notification': {
469-
'title': 'Push Tutorial',
470-
'body': 'Hello from device ID!',
471-
},
472-
'data': {'foo': 'bar', 'baz': 'qux'},
473-
},
474-
);
475-
_updateStatus('Push sent to device ID: ${device.id}');
476-
} catch (e) {
477-
_updateStatus('Failed to send push to device: $e');
478-
}
479-
}
480-
481-
Future<void> _sendPushToClient() async {
482-
try {
483-
final clientId = AblyService().realtime.auth.clientId;
484-
await AblyService().realtime.push.admin.publish(
485-
{'clientId': clientId},
486-
{
487-
'notification': {
488-
'title': 'Push Tutorial',
489-
'body': 'Hello from client ID!',
490-
},
491-
'data': {'foo': 'bar', 'baz': 'qux'},
492-
},
493-
);
494-
_updateStatus('Push sent to client ID: $clientId');
495-
} catch (e) {
496-
_updateStatus('Failed to send push to client: $e');
497-
}
498-
}
499-
```
500-
</Code>
501-
502-
Sending to a channel is just publishing a message with a `push` `extras` field:
461+
From the Flutter app itself, send push by publishing a message to a channel with a `push` `extras` field. Any device subscribed to that channel (via Step 3) will receive the notification. Add the following method to `_PushHomePageState`:
503462

504463
<Code>
505464
```flutter
506465
Future<void> _sendPushToChannel() async {
507466
try {
508467
final channel = AblyService().realtime.channels.get(_channelName);
509468
await channel.publish(
510-
ably.Message(
469+
message: ably.Message(
511470
name: 'example',
512471
data: 'Hello from channel!',
513472
extras: ably.MessageExtras({
@@ -529,32 +488,21 @@ Future<void> _sendPushToChannel() async {
529488
```
530489
</Code>
531490

532-
Add three more buttons to the `children` list in the `build` method, before the `Expanded` log widget:
491+
Add a button to the `children` list in the `build` method, before the `Expanded` log widget:
533492

534493
<Code>
535494
```flutter
536-
ElevatedButton(
537-
onPressed: _sendPushToDevice,
538-
style: ElevatedButton.styleFrom(backgroundColor: const Color(0xFF6610F2)),
539-
child: const Text('Send Push to Device',
540-
style: TextStyle(color: Colors.white)),
541-
),
542-
ElevatedButton(
543-
onPressed: _sendPushToClient,
544-
style: ElevatedButton.styleFrom(backgroundColor: const Color(0xFF20C997)),
545-
child: const Text('Send Push to Client',
546-
style: TextStyle(color: Colors.white)),
547-
),
548495
ElevatedButton(
549496
onPressed: _sendPushToChannel,
550-
style: ElevatedButton.styleFrom(backgroundColor: const Color(0xFF17A2B8)),
551-
child: const Text('Send Push to Channel',
552-
style: TextStyle(color: Colors.white)),
497+
style: ElevatedButton.styleFrom(
498+
backgroundColor: Colors.blue,
499+
foregroundColor: Colors.white),
500+
child: const Text('Send Push to Channel'),
553501
),
554502
```
555503
</Code>
556504

557-
Build and run the app again. Use the new buttons to send push notifications directly to your device ID, client ID, or the subscribed channel.
505+
Build and run the app again. Tap **Subscribe to Channel** first, then **Send Push to Channel** to send a push notification to all subscribed devices.
558506

559507
## Next steps <a id="next-steps"/>
560508

0 commit comments

Comments
 (0)