-
Notifications
You must be signed in to change notification settings - Fork 282
/
IntercomWrapper.m
165 lines (134 loc) · 5.47 KB
/
IntercomWrapper.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// IntercomWrapper.m
// Made
//
// Created by Asa Miller on 7/9/15.
// Copyright (c) 2015 Asa Miller. All rights reserved.
//
#import "IntercomWrapper.h"
#import <Intercom/Intercom.h>
@implementation IntercomWrapper
RCT_EXPORT_MODULE();
// Available as NativeModules.IntercomWrapper.registerIdentifiedUser
RCT_EXPORT_METHOD(registerIdentifiedUser:(NSDictionary*)options callback:(RCTResponseSenderBlock)callback) {
NSLog(@"registerIdentifiedUser with %@", options);
NSString* userId = options[@"userId"];
NSString* userEmail = options[@"email"];
if ([userId isKindOfClass:[NSNumber class]]) {
userId = [(NSNumber *)userId stringValue];
}
if (userId.length > 0 && userEmail.length > 0) {
[Intercom registerUserWithUserId:userId email:userEmail];
callback(@[[NSNull null], @[userId]]);
} else if (userId.length > 0) {
[Intercom registerUserWithUserId:userId];
callback(@[[NSNull null], @[userId]]);
} else if (userEmail.length > 0) {
[Intercom registerUserWithEmail:userEmail];
callback(@[[NSNull null], @[userId]]);
} else {
NSLog(@"[Intercom] ERROR - No user registered. You must supply an email, a userId or both");
callback(@[RCTMakeError(@"Error", nil, nil)]);
}
};
// Available as NativeModules.IntercomWrapper.registerUnidentifiedUser
RCT_EXPORT_METHOD(registerUnidentifiedUser:(RCTResponseSenderBlock)callback) {
NSLog(@"registerUnidentifiedUser");
[Intercom registerUnidentifiedUser];
callback(@[[NSNull null]]);
};
// Available as NativeModules.IntercomWrapper.reset
RCT_EXPORT_METHOD(reset:(RCTResponseSenderBlock)callback) {
NSLog(@"reset");
[Intercom reset];
callback(@[[NSNull null]]);
};
// Available as NativeModules.IntercomWrapper.updateUser
RCT_EXPORT_METHOD(updateUser:(NSDictionary*)options callback:(RCTResponseSenderBlock)callback) {
NSLog(@"updateUser with %@", options);
NSDictionary* attributes = options;
[Intercom updateUserWithAttributes:attributes];
callback(@[[NSNull null]]);
};
// Available as NativeModules.IntercomWrapper.logEvent
RCT_EXPORT_METHOD(logEvent:(NSString*)eventName metaData:(NSDictionary*)metaData callback:(RCTResponseSenderBlock)callback) {
NSLog(@"logEvent with %@", eventName);
if (metaData.count > 0) {
[Intercom logEventWithName:eventName metaData:metaData];
} else {
[Intercom logEventWithName:eventName];
}
callback(@[[NSNull null]]);
};
// Available as NativeModules.IntercomWrapper.displayMessageComposer
RCT_EXPORT_METHOD(displayMessageComposer:(RCTResponseSenderBlock)callback) {
NSLog(@"displayMessageComposer");
dispatch_async(dispatch_get_main_queue(), ^{
[Intercom presentMessageComposer];
});
callback(@[[NSNull null]]);
};
// Available as NativeModules.IntercomWrapper.displayConversationsList
RCT_EXPORT_METHOD(displayConversationsList:(RCTResponseSenderBlock)callback) {
NSLog(@"displayConversationsList");
dispatch_async(dispatch_get_main_queue(), ^{
[Intercom presentConversationList];
});
callback(@[[NSNull null]]);
};
// Available as NativeModules.IntercomWrapper.setVisibility
RCT_EXPORT_METHOD(setVisibility:(NSString*)visibilityString callback:(RCTResponseSenderBlock)callback) {
NSLog(@"setVisibility with %@", visibilityString);
BOOL hidden = NO;
if ([visibilityString isEqualToString:@"GONE"]) {
hidden = YES;
}
[Intercom setMessagesHidden:hidden];
callback(@[[NSNull null]]);
};
// Available as NativeModules.IntercomWrapper.setPreviewPosition
RCT_EXPORT_METHOD(setPreviewPosition:(NSString*)positionString callback:(RCTResponseSenderBlock)callback) {
NSLog(@"setPreviewPosition with %@", positionString);
ICMPreviewPosition previewPosition = ICMPreviewPositionBottomLeft;
if ([positionString isEqualToString:@"BOTTOM_RIGHT"]) {
previewPosition = ICMPreviewPositionBottomRight;
} else if ([positionString isEqualToString:@"TOP_RIGHT"]) {
previewPosition = ICMPreviewPositionBottomRight;
} else if ([positionString isEqualToString:@"TOP_LEFT"]) {
previewPosition = ICMPreviewPositionBottomLeft;
}
[Intercom setPreviewPosition:previewPosition];
callback(@[[NSNull null]]);
};
// Available as NativeModules.IntercomWrapper.setupAPN
RCT_EXPORT_METHOD(setupAPN:(NSString*)deviceToken callback:(RCTResponseSenderBlock)callback) {
NSLog(@"setupAPN with %@", deviceToken);
[Intercom setDeviceToken:[deviceToken dataUsingEncoding:NSUTF8StringEncoding]];
callback(@[[NSNull null]]);
};
// Available as NativeModules.IntercomWrapper.registerForPush
RCT_EXPORT_METHOD(registerForPush:(RCTResponseSenderBlock)callback) {
NSLog(@"registerForPush");
UIApplication *application = [UIApplication sharedApplication];
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]){ // iOS 8 (User notifications)
[application registerUserNotificationSettings:
[UIUserNotificationSettings settingsForTypes:
(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil]];
[application registerForRemoteNotifications];
} else { // iOS 7 (Remote notifications)
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationType)
(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
[Intercom registerForRemoteNotifications];
#pragma GCC diagnostic pop
callback(@[[NSNull null]]);
};
@end