registerPushToken method

Future<bool> registerPushToken({
  1. String? voipToken,
  2. String? fcmToken,
})

Register this device's push tokens (iOS VoIP / Android FCM) for incoming calls when the app is backgrounded/killed. The SDK does this automatically; call it yourself with a token if your app owns FCM/OneSignal and wants to hand the SDK the exact token. Returns true once a token was registered.

Implementation

Future<bool> registerPushToken({String? voipToken, String? fcmToken}) async {
  try {
    final ios = Platform.isIOS;
    // iOS rings via APNs VoIP (PushKit); Android via FCM. Only use the token
    // that platform actually uses, and treat an empty token as absent.
    var voip = ios ? (voipToken ?? await _callKit.voipToken()) : null;
    if (voip != null && voip.isEmpty) voip = null;
    var fcm = fcmToken;
    if (fcm == null && !ios) {
      try {
        fcm = await FirebaseMessaging.instance.getToken();
        _wireFcmHandlers(); // firebase is ready — auto-handle incoming pushes
      } catch (_) {/* firebase not ready — retry */}
    }
    if (fcm != null && fcm.isEmpty) fcm = null;

    // Not done until we have the platform's real token → keep retrying.
    final haveToken = ios ? voip != null : fcm != null;
    if (!haveToken) return false;

    await _post('/partner/voice/devices', {
      'identityId': _identityId,
      'deviceId': _deviceId,
      'platform': ios ? 'ios' : (Platform.isAndroid ? 'android' : 'other'),
      'voipToken': ?voip,
      'fcmToken': ?fcm,
    });
    NexusLog.info('voice: device registered for incoming push (fcm=${fcm != null}, voip=${voip != null})');
    return true;
  } catch (e) {
    NexusLog.warn('voice: device registration failed: $e');
    return false;
  }
}