import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intercom_flutter/intercom_flutter.dart';
import 'dart:convert';
import 'package:crypto/crypto.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class IntercomController extends GetxController {
@override
void onInit() async {
super.onInit();
try {
await Intercom.instance.initialize(
'pcb7up5t',
androidApiKey: 'android_sdk-73710fd800fea809597159a51d2ea6509cf67c40',
iosApiKey: 'ios_sdk-12eaefa4f4259339b1cbc416a589ab61442edd7f',
);
await Intercom.instance.logout();
} catch (e) {
if (kDebugMode) {
print('Error initializing Intercom: $e');
}
// Handle initialization error
}
}
String generateUserHash(String userId, String identitySecret) {
// Convert identitySecret to bytes
var key = utf8.encode(identitySecret);
// Convert userId to bytes
var bytes = utf8.encode(userId);
// Create HMAC-SHA256 object
var hmacSha256 = Hmac(sha256, key);
// Generate digest
var digest = hmacSha256.convert(bytes);
// Convert digest to hexadecimal string
return digest.toString();
}
Future<void> displayMessage() async {
try {
// Generate user hash
String userHash = generateUserHash('RAJANI-test', 'pBMzdPE0a08Y33P8mA5w_cUShAzJQ2sDK226a1w6');
print(userHash);
// Set user hash
await Intercom.instance.setUserHash(userHash);
await Intercom.instance.loginIdentifiedUser(userId: 'RAJANI-test');
// Now display the messenger
await Intercom.instance.displayMessenger();
} catch (e) {
if (kDebugMode) {
print('Error displaying Intercom messenger: $e');
}
// Optionally, show an error message to the user
}
}
}
class MyApp extends StatelessWidget {
final IntercomController intercomController = Get.put(IntercomController());
@override
Widget build(BuildContext context) {
return GetMaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Intercom example app'),
),
body: Center(
child: TextButton(
onPressed: () async {
await intercomController.displayMessage();
},
child: const Text('Show messenger'),
),
),
),
);
}
}