| .. | .. |
|---|
| 673 | 673 | onError?.call('Cannot send message: missing sessionId'); |
|---|
| 674 | 674 | } |
|---|
| 675 | 675 | |
|---|
| 676 | + /// Publish the APNs device token to the daemon for push notification delivery. |
|---|
| 677 | + /// The daemon stores it in ~/.aibroker/apns-tokens.json and uses it when |
|---|
| 678 | + /// no MQTT clients are connected (app is backgrounded or offline). |
|---|
| 679 | + void sendDeviceToken(String token) { |
|---|
| 680 | + final client = _client; |
|---|
| 681 | + if (client == null || client.connectionStatus?.state != MqttConnectionState.connected) { |
|---|
| 682 | + return; |
|---|
| 683 | + } |
|---|
| 684 | + try { |
|---|
| 685 | + final builder = MqttClientPayloadBuilder(); |
|---|
| 686 | + builder.addString('{"token":"$token","ts":${DateTime.now().millisecondsSinceEpoch}}'); |
|---|
| 687 | + client.publishMessage('pailot/device/token', MqttQos.atLeastOnce, builder.payload!); |
|---|
| 688 | + _mqttLog('Push: device token published to pailot/device/token'); |
|---|
| 689 | + } catch (e) { |
|---|
| 690 | + _mqttLog('Push: failed to publish device token: $e'); |
|---|
| 691 | + } |
|---|
| 692 | + } |
|---|
| 693 | + |
|---|
| 676 | 694 | /// Disconnect intentionally. |
|---|
| 677 | 695 | void disconnect() { |
|---|
| 678 | 696 | _intentionalClose = true; |
|---|