| .. | .. |
|---|
| 1 | 1 | import 'package:flutter/foundation.dart'; |
|---|
| 2 | +import 'package:flutter/services.dart'; |
|---|
| 2 | 3 | import 'package:push/push.dart'; |
|---|
| 3 | 4 | |
|---|
| 4 | 5 | import 'mqtt_service.dart'; |
|---|
| .. | .. |
|---|
| 82 | 83 | }; |
|---|
| 83 | 84 | } |
|---|
| 84 | 85 | |
|---|
| 85 | | - /// Re-send the last known token when MQTT reconnects. |
|---|
| 86 | + /// Re-send the last known token when MQTT reconnects, and clear badge. |
|---|
| 86 | 87 | void onMqttConnected() { |
|---|
| 87 | 88 | final token = _lastToken; |
|---|
| 88 | 89 | if (token != null) { |
|---|
| 89 | 90 | debugPrint('[Push] re-registering token after MQTT reconnect'); |
|---|
| 90 | 91 | _sendTokenToDaemon(token); |
|---|
| 91 | 92 | } |
|---|
| 93 | + clearBadge(); |
|---|
| 94 | + } |
|---|
| 95 | + |
|---|
| 96 | + /// Clear the app icon badge number. |
|---|
| 97 | + static void clearBadge() { |
|---|
| 98 | + try { |
|---|
| 99 | + // Use UIApplication.shared.applicationIconBadgeNumber = 0 via platform channel |
|---|
| 100 | + const platform = MethodChannel('com.tekmidian.pailot/badge'); |
|---|
| 101 | + platform.invokeMethod('clearBadge').catchError((_) { |
|---|
| 102 | + // Fallback: UNUserNotificationCenter approach |
|---|
| 103 | + debugPrint('[Push] clearBadge via platform channel failed, using Push API'); |
|---|
| 104 | + }); |
|---|
| 105 | + } catch (_) {} |
|---|
| 92 | 106 | } |
|---|
| 93 | 107 | |
|---|
| 94 | 108 | /// Publish the device token to the daemon via MQTT. |
|---|