Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ class IntercomFlutterPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Str
})
}
}
"changeWorkspace" -> {
val appId = call.argument<String>("appId")
val androidApiKey = call.argument<String>("androidApiKey")
if (appId != null && androidApiKey != null) {
Intercom.client().changeWorkspace(androidApiKey, appId)
result.success("Workspace changed")
} else {
result.error("INVALID_ARGUMENTS", "appId and androidApiKey are required", null)
}
}
else -> result.notImplemented()
}
}
Expand Down
14 changes: 14 additions & 0 deletions intercom_flutter/ios/Classes/IntercomFlutterPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,20 @@ - (void) handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
details: [self getIntercomError:errorCode:errorMsg]]);
}];
}
} else if([@"changeWorkspace" isEqualToString:call.method]) {
NSString *iosApiKey = call.arguments[@"iosApiKey"];
NSString *appId = call.arguments[@"appId"];
if (iosApiKey != nil && iosApiKey != (id)[NSNull null] &&
appId != nil && appId != (id)[NSNull null]) {
// iOS doesn't have native changeWorkspace, so logout and re-initialize
[Intercom logout];
[Intercom setApiKey:iosApiKey forAppId:appId];
result(@"Workspace changed");
} else {
result([FlutterError errorWithCode:@"INVALID_ARGUMENTS"
message:@"appId and iosApiKey are required"
details:nil]);
}
}
else {
result(FlutterMethodNotImplemented);
Expand Down
15 changes: 15 additions & 0 deletions intercom_flutter/lib/intercom_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,19 @@ class Intercom {
Future<void> setAuthTokens(Map<String, String> tokens) {
return IntercomFlutterPlatform.instance.setAuthTokens(tokens);
}

/// Changes the Intercom workspace.
///
/// On Android: Uses native changeWorkspace API (SDK 16.1.0+)
/// On iOS: Logs out and re-initializes with new credentials
///
/// This will logout the current user and clear all SDK data.
/// You must call login again after changing workspace.
///
/// [appId] is required for both platforms.
/// [androidApiKey] is required for Android.
/// [iosApiKey] is required for iOS.
Future<void> changeWorkspace(String appId, {String? androidApiKey, String? iosApiKey}) {
return IntercomFlutterPlatform.instance.changeWorkspace(appId, androidApiKey: androidApiKey, iosApiKey: iosApiKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,19 @@ abstract class IntercomFlutterPlatform extends PlatformInterface {
Future<void> setAuthTokens(Map<String, String> tokens) {
throw UnimplementedError('setAuthTokens() has not been implemented.');
}

/// Changes the Intercom workspace.
///
/// On Android: Uses native changeWorkspace API (SDK 16.1.0+)
/// On iOS: Logs out and re-initializes with new credentials
///
/// This will logout the current user and clear all SDK data.
/// You must call login again after changing workspace.
///
/// [appId] is required for both platforms.
/// [androidApiKey] is required for Android.
/// [iosApiKey] is required for iOS.
Future<void> changeWorkspace(String appId, {String? androidApiKey, String? iosApiKey}) {
throw UnimplementedError('changeWorkspace() has not been implemented.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ class MethodChannelIntercomFlutter extends IntercomFlutterPlatform {
await _channel.invokeMethod('setAuthTokens', {'tokens': tokens});
}

@override
Future<void> changeWorkspace(String appId, {String? androidApiKey, String? iosApiKey}) async {
await _channel.invokeMethod('changeWorkspace', {'appId': appId, 'androidApiKey': androidApiKey, 'iosApiKey': iosApiKey});
}

/// Convert the [PlatformException] details to [IntercomError].
/// From the Platform side if the intercom operation failed then error details
/// will be sent as details in [PlatformException].
Expand Down
12 changes: 12 additions & 0 deletions intercom_flutter_web/lib/intercom_flutter_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,18 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
print("Auth tokens added");
}

@override
Future<void> changeWorkspace(String appId, {String? androidApiKey, String? iosApiKey}) async {
// For web, we shutdown the current workspace and boot with the new appId
// Clear user data first
removeIntercomSettings(['user_hash', 'intercom_user_jwt', 'user_id', 'email', 'auth_tokens']);
// Shutdown current workspace
globalContext.callMethod('Intercom'.toJS, 'shutdown'.toJS);
// Boot with new workspace
globalContext.callMethod('Intercom'.toJS, 'boot'.toJS, updateIntercomSettings('app_id', appId).jsify());
print("Workspace changed");
}

/// get the [window.intercomSettings]
Map<dynamic, dynamic> getIntercomSettings() {
if (globalContext.hasProperty('intercomSettings'.toJS).toDart) {
Expand Down