preTesting
This commit is contained in:
66
lib/BlindMasterResources/apns_service.dart
Normal file
66
lib/BlindMasterResources/apns_service.dart
Normal file
@@ -0,0 +1,66 @@
|
||||
import 'package:blind_master/BlindMasterResources/secure_transmissions.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
/// Bridge to the native iOS APNs registration in `ios/Runner/AppDelegate.swift`.
|
||||
/// The Swift side captures the device token in
|
||||
/// `didRegisterForRemoteNotificationsWithDeviceToken`, hex-encodes it, and
|
||||
/// pushes it back here over the `blindmaster/apns` MethodChannel. We then POST
|
||||
/// it to `/register_apns_token` so the server can target this device.
|
||||
///
|
||||
/// Mirrors the LockInBroMobile NotificationService flow — Android has no APNs
|
||||
/// equivalent and the backend only supports APNs, so this is a no-op there.
|
||||
class ApnsService {
|
||||
static const _channel = MethodChannel('blindmaster/apns');
|
||||
static bool _handlerInstalled = false;
|
||||
|
||||
/// Wire the channel handler once at startup so a token delivered before the
|
||||
/// first `register()` call (or after, asynchronously) is still uploaded.
|
||||
static void install() {
|
||||
if (_handlerInstalled) return;
|
||||
if (defaultTargetPlatform != TargetPlatform.iOS &&
|
||||
defaultTargetPlatform != TargetPlatform.macOS) {
|
||||
return;
|
||||
}
|
||||
_handlerInstalled = true;
|
||||
_channel.setMethodCallHandler((call) async {
|
||||
if (call.method == 'onToken') {
|
||||
final token = call.arguments as String?;
|
||||
if (token == null || token.isEmpty) return;
|
||||
debugPrint('APNS: token received');
|
||||
try {
|
||||
await securePost({'token': token}, 'register_apns_token');
|
||||
} catch (e) {
|
||||
debugPrint('APNS: token upload failed: $e');
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Request notification permission and upload the cached token if APNs has
|
||||
/// already produced one. Safe to call repeatedly (login + session-restore) —
|
||||
/// the server upserts.
|
||||
static Future<void> register() async {
|
||||
if (defaultTargetPlatform != TargetPlatform.iOS &&
|
||||
defaultTargetPlatform != TargetPlatform.macOS) {
|
||||
return;
|
||||
}
|
||||
install();
|
||||
try {
|
||||
final granted =
|
||||
await _channel.invokeMethod<bool>('requestPermission') ?? false;
|
||||
if (!granted) {
|
||||
debugPrint('APNS: notifications denied — enable in Settings');
|
||||
return;
|
||||
}
|
||||
final token = await _channel.invokeMethod<String?>('getToken');
|
||||
if (token == null || token.isEmpty) {
|
||||
debugPrint('APNS: token not ready yet — onToken handler will catch it');
|
||||
return;
|
||||
}
|
||||
await securePost({'token': token}, 'register_apns_token');
|
||||
} catch (e) {
|
||||
debugPrint('APNS registration failed: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
import 'package:blind_master/BlindMasterResources/secure_transmissions.dart';
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
class FcmService {
|
||||
/// Request permission, fetch the FCM token, and register it with the server.
|
||||
/// Safe to call on every login/session-restore — the server just upserts the value.
|
||||
static Future<void> register() async {
|
||||
debugPrint('FCM: register() called');
|
||||
try {
|
||||
final messaging = FirebaseMessaging.instance;
|
||||
final settings = await messaging.requestPermission(alert: true, badge: true, sound: true);
|
||||
debugPrint('FCM: authorization status: ${settings.authorizationStatus}');
|
||||
if (settings.authorizationStatus == AuthorizationStatus.denied) {
|
||||
debugPrint('FCM: notifications denied — enable in Settings > [App] > Notifications');
|
||||
return;
|
||||
}
|
||||
await messaging.setForegroundNotificationPresentationOptions(
|
||||
alert: true,
|
||||
badge: true,
|
||||
sound: true,
|
||||
);
|
||||
// On iOS, APNs token must be available before FCM token can be fetched.
|
||||
// getAPNSToken() can block if iOS hasn't finished APNs registration yet,
|
||||
// so cap it with a timeout and retry once after a short delay.
|
||||
if (defaultTargetPlatform == TargetPlatform.iOS ||
|
||||
defaultTargetPlatform == TargetPlatform.macOS) {
|
||||
String? apnsToken = await messaging.getAPNSToken()
|
||||
.timeout(const Duration(seconds: 3), onTimeout: () => null);
|
||||
if (apnsToken == null) {
|
||||
debugPrint('FCM: APNs token not ready, retrying in 5s...');
|
||||
await Future.delayed(const Duration(seconds: 5));
|
||||
apnsToken = await messaging.getAPNSToken()
|
||||
.timeout(const Duration(seconds: 5), onTimeout: () => null);
|
||||
}
|
||||
if (apnsToken == null) {
|
||||
debugPrint('FCM: APNs token unavailable — simulator or APNs not configured');
|
||||
return;
|
||||
}
|
||||
debugPrint('FCM: APNs token acquired');
|
||||
}
|
||||
final token = await messaging.getToken();
|
||||
debugPrint('FCM TOKEN: ${token ?? "null — likely running on simulator"}');
|
||||
if (token == null) return;
|
||||
await securePost({'token': token}, 'register_fcm_token');
|
||||
} catch (e) {
|
||||
debugPrint('FCM registration failed: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ String host = local;
|
||||
int port = 3000;
|
||||
String priv = "$scheme://$host:$port";
|
||||
|
||||
String pub = "https://wahwa.com";
|
||||
String pub = "https://blindmaster.wahwa.com";
|
||||
|
||||
String socketString = pub;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user