Skip to content

Commit 735d4bb

Browse files
carlosa54browniefed
authored andcommitted
Add error handling and refactor native modules to use Promises (#265)
* Remove IntercomIntentService and reference from README in favor of deprecating GCM notifications * Remove GCM reference * Adds error handling when calling Intercom.client(), without this the module will crash the Main app if Intercom hasn't been initialized * sendTokenToIntercom error handling * Add support for initializing Intercom from javascript * Refactor Android module from using Callbacks to Promises * Refactor iOS module from using Callbacks to use Promises * Modify IntercomClient js to expect promises from native modules * Add initializeIntercom to ts * Remove initializeIntercom * Add example for existing FCM setup to README
1 parent 3402a6c commit 735d4bb

File tree

6 files changed

+298
-499
lines changed

6 files changed

+298
-499
lines changed

README.md

Lines changed: 84 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -103,40 +103,6 @@ React Native wrapper for Intercom.io. Based off of [intercom-cordova](https://gi
103103
}
104104
```
105105
106-
1. In `android/app/src/main/AndroidManifest.xml`, add the following code in the respective sections of the file:
107-
108-
```xml
109-
<?xml version="1.0" encoding="utf-8"?>
110-
<manifest package="com.myapp"
111-
112-
...other configuration here...
113-
114-
>
115-
<application
116-
117-
...other configuration here...
118-
119-
xmlns:tools="http://schemas.android.com/tools"
120-
>
121-
122-
<!-- ...other configuration here... -->
123-
124-
<service
125-
android:name="com.robinpowered.react.Intercom.IntercomIntentService"
126-
android:exported="false">
127-
<intent-filter
128-
android:priority="999">
129-
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
130-
</intent-filter>
131-
</service>
132-
<receiver
133-
android:name="io.intercom.android.sdk.push.IntercomPushBroadcastReceiver"
134-
tools:replace="android:exported"
135-
android:exported="true" />
136-
</application>
137-
</manifest>
138-
```
139-
140106
1. In `android/build.gradle` add `maven { url "https://maven.google.com" }` ([h/t](https://github.com/tinycreative/react-native-intercom/issues/153#issuecomment-348602868)):
141107
142108
```gradle
@@ -150,36 +116,90 @@ React Native wrapper for Intercom.io. Based off of [intercom-cordova](https://gi
150116
}
151117
```
152118
153-
1. Decide which type of push messaging you want to install, and add choosen method to `android/app/build.gradle`.
154-
155-
If "Google Cloud Messaging (GCM)", then:
156-
157-
```gradle
158-
dependencies {
159-
160-
//...other configuration here...
161-
162-
compile 'io.intercom.android:intercom-sdk:5.+'
163-
}
164-
```
165-
If "Firebase Cloud Messaging(FCM)", then:
166-
167-
```gradle
168-
dependencies {
169-
170-
//...other configuration here...
171-
172-
compile 'io.intercom.android:intercom-sdk-fcm:5.+'
173-
compile 'com.google.firebase:firebase-messaging:11.+'
174-
}
175-
```
176-
If you'd rather not have push notifications in your app, you can use this dependency:
177-
178-
```gradle
179-
dependencies {
180-
implementation 'io.intercom.android:intercom-sdk-base:5.+'
181-
}
182-
```
119+
1. Decide which type of push messaging you want to install, and add choosen method to `android/app/build.gradle`.
120+
121+
1. If you'd rather not have push notifications in your app, you can use this dependency:
122+
123+
```gradle
124+
dependencies {
125+
implementation 'io.intercom.android:intercom-sdk-base:5.+'
126+
}
127+
```
128+
129+
1. If "Firebase Cloud Messaging(FCM)", then:
130+
```gradle
131+
dependencies {
132+
133+
//...other configuration here...
134+
135+
compile 'io.intercom.android:intercom-sdk-fcm:5.+'
136+
}
137+
```
138+
139+
If you're already using FCM in your application you'll need to extend `FirebaseMessagingService` to handle Intercom's push notifications (refer to [Using Intercom with other FCM setups](https://developers.intercom.com/installing-intercom/docs/android-fcm-push-notifications#section-step-7-using-intercom-with-other-fcm-setups-optional))
140+
141+
### Here's an example if you're using [react-native-firebase](https://github.com/invertase/react-native-firebase) as your existing FCM setup:
142+
143+
I. Add a new file if you don't have one (`android/app/src/main/java/com/YOUR_APP/MainMessagingService.java`)
144+
145+
```java
146+
package com.YOUR_APP;
147+
import io.invertase.firebase.messaging.*;
148+
import android.content.Intent;
149+
import android.content.Context;
150+
import io.intercom.android.sdk.push.IntercomPushClient;
151+
import io.invertase.firebase.messaging.RNFirebaseMessagingService;
152+
import com.google.firebase.messaging.RemoteMessage;
153+
import android.util.Log;
154+
import java.util.Map;
155+
156+
public class MainMessagingService extends RNFirebaseMessagingService {
157+
private static final String TAG = "MainMessagingService";
158+
private final IntercomPushClient intercomPushClient = new IntercomPushClient();
159+
160+
@Override
161+
public void onMessageReceived(RemoteMessage remoteMessage) {
162+
Map message = remoteMessage.getData();
163+
164+
if (intercomPushClient.isIntercomPush(message)) {
165+
Log.d(TAG, "Intercom message received");
166+
intercomPushClient.handlePush(getApplication(), message);
167+
} else {
168+
super.onMessageReceived(remoteMessage);
169+
}
170+
}
171+
}
172+
```
173+
174+
II. Then add the following code to `android/app/src/main/AndroidManifest.xml`:
175+
176+
```xml
177+
<?xml version="1.0" encoding="utf-8"?>
178+
<manifest package="com.YOUR_APP"
179+
180+
...other configuration here...
181+
182+
>
183+
<application
184+
185+
...other configuration here...
186+
187+
xmlns:tools="http://schemas.android.com/tools"
188+
>
189+
190+
<!-- ...other configuration here... -->
191+
<!-- ...ADD THE SERVICE BELOW... -->
192+
<service
193+
android:name=".MainMessagingService"
194+
android:enabled="true"
195+
android:exported="true">
196+
<intent-filter>
197+
<action android:name="com.google.firebase.MESSAGING_EVENT" />
198+
</intent-filter>
199+
</service>
200+
</application>
201+
</manifest>
202+
```
183203
184204
1. Import Intercom and use methods
185205

android/src/main/java/com/robinpowered/react/Intercom/IntercomEventEmitter.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public class IntercomEventEmitter extends ReactContextBaseJavaModule {
2828

2929
public IntercomEventEmitter(ReactApplicationContext reactContext) {
3030
super(reactContext);
31-
Intercom.client().addUnreadConversationCountListener(unreadConversationCountListener);
31+
try {
32+
Intercom.client().addUnreadConversationCountListener(unreadConversationCountListener);
33+
} catch (Exception e) {
34+
Log.e(TAG, "client called before Intercom initialization");
35+
}
3236
}
3337

3438
@Override
@@ -44,9 +48,13 @@ public Map<String, Object> getConstants() {
4448
}
4549

4650
private void handleUpdateUnreadCount() {
47-
WritableMap params = Arguments.createMap();
48-
params.putInt("count", Intercom.client().getUnreadConversationCount());
49-
sendEvent(UNREAD_CHANGE_NOTIFICATION, params);
51+
try {
52+
WritableMap params = Arguments.createMap();
53+
params.putInt("count", Intercom.client().getUnreadConversationCount());
54+
sendEvent(UNREAD_CHANGE_NOTIFICATION, params);
55+
} catch (Exception e) {
56+
Log.e(TAG, "client called before Intercom initialization");
57+
}
5058
}
5159

5260
private void sendEvent(String eventName, @Nullable WritableMap params) {

android/src/main/java/com/robinpowered/react/Intercom/IntercomIntentService.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)