- Extract
react-native-freshchat-sdk
and place it insidenode_module
folder in your project - Add
"react-native-freshchat-sdk": "^0.5.7"
underdependencies
in projectpackage.json
- Run
react-native link
on you project root directory
- Navigate inside
ios
folder in your project - Add an entry for FreshchatSDK as shown below in
Podfile
target 'ProjectName' do pod 'FreshchatSDK', :path=> '../node_modules/react-native-freshchat-sdk/ios/FreshchatSDK.podspec' end
- Run
pod install
from ios directory
Follow how to manually link a library here https://facebook.github.io/react-native/docs/linking-libraries-ios.html#content
Add maven { url "https://jitpack.io” }
to you project level build.gradle as below
allprojects {
repositories {
.....
maven { url "https://jitpack.io" }
}
}
import {
Freshchat,
FreshchatConfig,
FaqOptions,
ConversationOptions,
FreshchatUser,
FreshchatMessage,
FreshchatNotificationConfig
} from 'react-native-freshchat-sdk';
var freshchatConfig = new FreshchatConfig('YOUR_APP_ID', 'YOUR_APP_KEY');
Freshchat.init(freshchatConfig);
Freshchat.showFAQs();
var faqOptions = new FaqOptions();
faqOptions.tags = ["premium"];
faqOptions.filteredViewTitle = "Tags";
faqOptions.filterType = FaqOptions.FilterType.ARTICLE;
Freshchat.showFAQs(faqOptions);
Freshchat.showConversations();
var conversationOptions = new ConversationOptions();
conversationOptions.tags = ["premium"];
conversationOptions.filteredViewTitle = "Premium Support";
Freshchat.showConversations(conversationOptions);
Freshchat.resetUser();
Freshchat.getUnreadCountAsync((data) => {
console.log(data);
});
Freshchat.addEventListener(
Freshchat.EVENT_UNREAD_MESSAGE_COUNT_CHANGED,
() => {
console.log("onUnreadMessageCountChanged triggered");
Freshchat.getUnreadCountAsync((data) => {
var count = data.count;
var status = data.status;
if (status) {
console.log("Message count: " + count);
} else {
console.log("getUnreadCountAsync unsuccessful");
}
});
}
);
Freshchat.removeEventListeners(Freshchat.EVENT_UNREAD_MESSAGE_COUNT_CHANGED);
Freshchat.getUser((user) => {
console.log(user);
})
var freshchatUser = new FreshchatUser();
freshchatUser.firstName = "John";
freshchatUser.lastName = "Doe";
freshchatUser.email = "[email protected]";
freshchatUser.phoneCountryCode = "+91";
freshchatUser.phone = "1234234123";
Freshchat.setUser(freshchatUser, (error) => {
console.log(error);
});
var userPropertiesJson = {
"user_type": "Paid",
"plan": "Gold"
}
Freshchat.setUserProperties(userPropertiesJson, (error) => {
console.log(error);
});
Freshchat.getSDKVersionCode((data) => {
console.log(data);
});
var freshchatMessage = new FreshchatMessage();
freshchatMessage.tag = "video";
freshchatMessage.message = "text send message";
Freshchat.sendMessage(freshchatMessage);
Freshchat.dismissFreshchatViews();
Freshchat.identifyUser("EXTERNAL_ID", "RESTORE_ID", (error) => {
console.log(error);
});
Freshchat.addEventListener(
Freshchat.EVENT_USER_RESTORE_ID_GENERATED,
() => {
console.log("onRestoreIdUpdated triggered");
Freshchat.getUser((user) => {
var restoreId = user.restoreId;
var externalId = user.externalId;
console.log("externalId: " + externalId);
console.log("restoreId: " + restoreId);
})
}
);
Freshchat.removeEventListeners(Freshchat.EVENT_USER_RESTORE_ID_GENERATED);
var freshchatNotificationConfig = new FreshchatNotificationConfig();
freshchatNotificationConfig.priority = FreshchatNotificationConfig.NotificationPriority.PRIORITY_HIGH;
freshchatNotificationConfig.notificationSoundEnabled = false;
Freshchat.setNotificationConfig(freshchatNotificationConfig);
Freshchat.setPushRegistrationToken(token);
Freshchat.isFreshchatNotification(notification, (freshchatNotification) => {
if (freshchatNotification) {
Freshchat.handlePushNotification(notification);
} else {
// handle your app notification
}
})
In iOS, Application state should be sent as part of notification payload to effectively handle notification
isActive
field should be true
if Application state is active, false
otherwise.
NSMutableDictionary *mutableDict = [response.notification.request.content.userInfo mutableCopy];
BOOL isActive = [UIApplication sharedApplication].applicationState == UIApplicationStateActive;
[mutableDict setObject:@(isActive) forKey:@"isActive"];
Note: If this parameter is not set, Conversations screen will not open when app is inactive or in background. Instead In-App notification will be shown.
const notificationPayload = Freshchat.transformPushNotificationIOSPayloadToNativePayload(notification);
If you want to handle push notifications in Native layer. You can follow the below steps
import "FreshchatSDK.h"
[[FreshchatSDK sharedInstance]setPushRegistrationToken:deviceToken];
if ([[FreshchatSDK sharedInstance]isFreshchatNotification:userInfo]) {
[[FreshchatSDK sharedInstance]handlePushNotification:userInfo];
} else {
// handle your app notification
}
Freshchat.getInstance(context).setPushRegistrationToken(token);
if (Freshchat.isFreshchatNotification(message)) {
Freshchat.getInstance(getApplicationContext()).handleFcmMessage(message);
} else {
// handle your app notification
}
-
Add event listener for Freshchat.EVENT_USER_INTERACTED event. When user interacts with app, this event will be triggered.
Freshchat.addEventListener( Freshchat.EVENT_USER_INTERACTED, userInteractionHandler );
-
iOS Specific configuration
-
In AppDelegate.h, Use
RNFreshchatWindow
instead ofUIWindow
, which extendsUIWindow
#import "RNFreshchatSdk.h" @property (nonatomic, strong) RNFreshchatWindow *window;
-
In AppDelegate.m,
- Use
RNFreshchatWindow
instead ofUIWindow
- Override
applicationDidEnterBackground
and triggerFRESHCHAT_USER_INTERACTED
event as below- (void)applicationDidEnterBackground:(UIApplication *)application { [[NSNotificationCenter defaultCenter] postNotificationName:FRESHCHAT_USER_INTERACTED object:self]; }
- Use
-
-
To remove event listener, use the below code
Freshchat.removeEventListeners(Freshchat.EVENT_USER_INTERACTED);
- To create user with reference_id and to restore existing user, call
restoreUserWithIdToken
with JWT token. - To update user properties, create user without reference_id or to update JWT token, call
setUserWithIdToken
with JWT token. getUserIdTokenStatus
return current status of the JWT tokengetFreshchatUserId
generate and return user alias- Use
Freshchat.FRESHCHAT_EVENTS
event to listen to user interactions like SCREEN_TRANSITIONS and NEW_MESSAGE.
JWT Token will be in any one of the below 5 states
- TOKEN_NOT_SET : Token not set in SDK
- TOKEN_NOT_PROCESSED : Token set in SDK and is being processed
- TOKEN_VALID : Token set and is valid
- TOKEN_INVALID : Token set and is invalid
- TOKEN_EXPIRED : Token set and is expired
Freshchat.restoreUserWithIdToken(jwt);
Freshchat.setUserWithIdToken(jwt);
Freshchat.getUserIdTokenStatus((data) => {
var status = data.user_id_token_status;
console.log('FRESHCHAT_EVENTS: state - ', status);
});
Freshchat.getFreshchatUserId((alias) => {
console.log('Alias - ', alias);
});
Adding Listener
Freshchat.addEventListener(
Freshchat.FRESHCHAT_EVENTS,
userActionsEventHandler
);
Handling when event triggered
const userActionsEventHandler = (actionData) => {
console.log("FRESHCHAT_EVENTS triggered");
var action = actionData.user_action;
Freshchat.getUserIdTokenStatus((data) => {
var status = data.user_id_token_status;
console.log('FRESHCHAT_EVENTS: action - ', action);
console.log('FRESHCHAT_EVENTS: state - ', status);
});
};
Adding listener
Freshchat.addEventListener(
Freshchat.EVENT_EXTERNAL_LINK_CLICKED,
openLinkHandler
);
Handling when event triggered
const openLinkHandler = (data) => {
console.log("link - ", data.url);
};
Freshchat.addEventListener(
Freshchat.FRESHCHAT_NOTIFICATION_CLICKED,
(data) => {
// if (canOpenSdkScreenDirectly) {
Freshchat.openFreshchatDeeplink(data.url);
// } else {
// save deeplink and open Freshchat screen when required
// }
}
);
When app is in killed state and SDK screen is opened directly due to notifications, activityToLaunchOnFinish
will be launched when user clicks back button from SDK screen.
Set overrideNotificationClickListener
to true
when you want to override notification click event in Android.
Configure FreshchatNotificationConfig as follows.
var freshchatNotificationConfig = new FreshchatNotificationConfig();
freshchatNotificationConfig.priority = FreshchatNotificationConfig.NotificationPriority.PRIORITY_HIGH;
freshchatNotificationConfig.notificationSoundEnabled = false;
freshchatNotificationConfig.activityToLaunchOnFinish = "com.demoapp.MainActivity";
freshchatNotificationConfig.overrideNotificationClickListener = true;
Freshchat.setNotificationConfig(freshchatNotificationConfig);
More instructions here: Freshchat for Android
More instructions here: Freshchat for iOS