Skip to content

Commit 575bc23

Browse files
committed
feat: Update notifications for android to use launcher activity by default when going back
1 parent 02da227 commit 575bc23

File tree

3 files changed

+100
-53
lines changed

3 files changed

+100
-53
lines changed

README.md

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -173,31 +173,61 @@ apply from: file("../../node_modules/@react-native-community/cli-platform-androi
173173
- Create `MainNotificationService.java` inside your app directory(`com.example.app`) with below content:
174174

175175
***Remember to replace `package com.example.app;`, with your app package name***
176+
- Default Notification Behavior (Goes back to the parent default launcher activity when the user taps the notification):
176177

177-
```java
178-
package com.example.app;
178+
```java
179+
package com.example.app;
179180

180-
import com.google.firebase.messaging.FirebaseMessagingService;
181-
import com.google.firebase.messaging.RemoteMessage;
182-
import com.intercom.reactnative.IntercomModule;
181+
import com.google.firebase.messaging.FirebaseMessagingService;
182+
import com.google.firebase.messaging.RemoteMessage;
183+
import com.intercom.reactnative.IntercomModule;
183184

184-
public class MainNotificationService extends FirebaseMessagingService {
185+
public class MainNotificationService extends FirebaseMessagingService {
185186

186-
@Override
187-
public void onNewToken(String refreshedToken) {
188-
IntercomModule.sendTokenToIntercom(getApplication(), refreshedToken);
189-
//DO LOGIC HERE
190-
}
187+
@Override
188+
public void onNewToken(String refreshedToken) {
189+
IntercomModule.sendTokenToIntercom(getApplication(), refreshedToken);
190+
//DO LOGIC HERE
191+
}
191192

192-
public void onMessageReceived(RemoteMessage remoteMessage) {
193-
if (IntercomModule.isIntercomPush(remoteMessage)) {
194-
IntercomModule.handleRemotePushMessage(getApplication(), remoteMessage);
195-
} else {
196-
// HANDLE NON-INTERCOM MESSAGE
193+
public void onMessageReceived(RemoteMessage remoteMessage) {
194+
if (IntercomModule.isIntercomPush(remoteMessage)) {
195+
IntercomModule.handleRemotePushMessage(getApplication(), remoteMessage);
196+
} else {
197+
// HANDLE NON-INTERCOM MESSAGE
198+
}
199+
}
197200
}
198-
}
199-
}
200-
```
201+
```
202+
203+
- Custom Stack:
204+
205+
```java
206+
package com.example.app;
207+
208+
import com.google.firebase.messaging.FirebaseMessagingService;
209+
import com.google.firebase.messaging.RemoteMessage;
210+
import com.intercom.reactnative.IntercomModule;
211+
212+
public class MainNotificationService extends FirebaseMessagingService {
213+
214+
@Override
215+
public void onNewToken(String refreshedToken) {
216+
IntercomModule.sendTokenToIntercom(getApplication(), refreshedToken);
217+
//DO LOGIC HERE
218+
}
219+
220+
public void onMessageReceived(RemoteMessage remoteMessage) {
221+
if (IntercomModule.isIntercomPush(remoteMessage)) {
222+
TaskStackBuilder customStack = TaskStackBuilder.create(getApplication());
223+
customStack.addNextIntent(new Intent(getApplication(), MainActivity.class)); // Replace with your custom activity
224+
IntercomModule.handleRemotePushWithCustomStack(getApplication(), remoteMessage, customStack);
225+
} else {
226+
// HANDLE NON-INTERCOM MESSAGE
227+
}
228+
}
229+
}
230+
```
201231

202232
- Edit `AndroidManifest.xml`. Add below content inside `<application>` below `<activity/>`
203233

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,5 @@ dependencies {
6969
//noinspection GradleDynamicVersion
7070
implementation "com.facebook.react:react-native:+" // From node_modules
7171
implementation "com.google.firebase:firebase-messaging:${safeExtGet('firebaseMessagingVersion', '20.2.+')}"
72-
implementation 'io.intercom.android:intercom-sdk:17.0.3'
72+
implementation 'io.intercom.android:intercom-sdk:17.1.0'
7373
}

android/src/main/java/com/intercom/reactnative/IntercomModule.java

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Activity;
44
import android.app.Application;
5+
import android.content.Intent;
56
import android.util.Log;
67
import android.widget.Toast;
78

@@ -37,6 +38,7 @@
3738
import io.intercom.android.sdk.helpcenter.sections.HelpCenterCollectionContent;
3839
import io.intercom.android.sdk.identity.Registration;
3940
import io.intercom.android.sdk.push.IntercomPushClient;
41+
import android.app.TaskStackBuilder;
4042

4143
@ReactModule(name = IntercomModule.NAME)
4244
public class IntercomModule extends ReactContextBaseJavaModule {
@@ -65,11 +67,25 @@ public static boolean isIntercomPush(RemoteMessage remoteMessage) {
6567
}
6668
}
6769

68-
public static void handleRemotePushMessage(@NonNull Application application, RemoteMessage
69-
remoteMessage) {
70+
public static void handleRemotePushWithCustomStack(@NonNull Application application, RemoteMessage remoteMessage,
71+
TaskStackBuilder customStack) {
7072
try {
71-
Map message = remoteMessage.getData();
72-
intercomPushClient.handlePush(application, message);
73+
Map<String, String> message = remoteMessage.getData();
74+
intercomPushClient.handlePushWithCustomStack(application, message, customStack);
75+
} catch (Exception err) {
76+
Log.e(NAME, "handlePushWithCustomStack error:");
77+
Log.e(NAME, err.toString());
78+
}
79+
}
80+
81+
public static void handleRemotePushMessage(@NonNull Application application, RemoteMessage remoteMessage) {
82+
try {
83+
TaskStackBuilder customStack = TaskStackBuilder.create(application);
84+
Intent launchIntent = application.getPackageManager().getLaunchIntentForPackage(application.getPackageName());
85+
if (launchIntent != null) {
86+
customStack.addNextIntent(launchIntent);
87+
}
88+
handleRemotePushWithCustomStack(application, remoteMessage, customStack);
7389
} catch (Exception err) {
7490
Log.e(NAME, "handleRemotePushMessage error:");
7591
Log.e(NAME, err.toString());
@@ -107,8 +123,7 @@ public void sendTokenToIntercom(@NonNull String token, Promise promise) {
107123
Log.e(NAME, "no current activity");
108124
}
109125

110-
} catch (
111-
Exception err) {
126+
} catch (Exception err) {
112127
Log.e(NAME, "sendTokenToIntercom error:");
113128
Log.e(NAME, err.toString());
114129
promise.reject(IntercomErrorCodes.SEND_TOKEN_TO_INTERCOM, err.toString());
@@ -117,24 +132,25 @@ public void sendTokenToIntercom(@NonNull String token, Promise promise) {
117132

118133
@ReactMethod
119134
public void loginUnidentifiedUser(Promise promise) {
120-
Intercom.client().loginUnidentifiedUser(new IntercomStatusCallback() {
121-
@Override
122-
public void onSuccess() {
123-
promise.resolve(true);
124-
}
135+
Intercom.client().loginUnidentifiedUser(new IntercomStatusCallback() {
136+
@Override
137+
public void onSuccess() {
138+
promise.resolve(true);
139+
}
125140

126-
@Override
127-
public void onFailure(@NonNull IntercomError intercomError) {
128-
Log.e("ERROR", intercomError.getErrorMessage());
129-
promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage());
130-
}
131-
});
141+
@Override
142+
public void onFailure(@NonNull IntercomError intercomError) {
143+
Log.e("ERROR", intercomError.getErrorMessage());
144+
promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage());
145+
}
146+
});
132147
}
133148

134149
@ReactMethod
135150
public void loginUserWithUserAttributes(ReadableMap params, Promise promise) {
136151
Boolean hasEmail = params.hasKey("email") && IntercomHelpers.getValueAsStringForKey(params, "email").length() > 0;
137-
Boolean hasUserId = params.hasKey("userId") && IntercomHelpers.getValueAsStringForKey(params, "userId").length() > 0;
152+
Boolean hasUserId = params.hasKey("userId")
153+
&& IntercomHelpers.getValueAsStringForKey(params, "userId").length() > 0;
138154
Registration registration = null;
139155
if (hasEmail && hasUserId) {
140156
String email = IntercomHelpers.getValueAsStringForKey(params, "email");
@@ -181,19 +197,19 @@ public void setUserHash(String userHash, Promise promise) {
181197

182198
@ReactMethod
183199
public void updateUser(ReadableMap params, Promise promise) {
184-
UserAttributes userAttributes = IntercomHelpers.buildUserAttributes(params);
185-
Intercom.client().updateUser(userAttributes, new IntercomStatusCallback() {
186-
@Override
187-
public void onSuccess() {
188-
promise.resolve(true);
189-
}
200+
UserAttributes userAttributes = IntercomHelpers.buildUserAttributes(params);
201+
Intercom.client().updateUser(userAttributes, new IntercomStatusCallback() {
202+
@Override
203+
public void onSuccess() {
204+
promise.resolve(true);
205+
}
190206

191-
@Override
192-
public void onFailure(@NonNull IntercomError intercomError) {
193-
Log.e("ERROR", intercomError.getErrorMessage());
194-
promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage());
195-
}
196-
});
207+
@Override
208+
public void onFailure(@NonNull IntercomError intercomError) {
209+
Log.e("ERROR", intercomError.getErrorMessage());
210+
promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage());
211+
}
212+
});
197213
}
198214

199215
@ReactMethod
@@ -358,7 +374,6 @@ public void presentContent(ReadableMap params, Promise promise) {
358374
}
359375
}
360376

361-
362377
@ReactMethod
363378
public void fetchHelpCenterCollections(Promise promise) {
364379
try {
@@ -400,7 +415,8 @@ public void fetchHelpCenterCollection(String collectionId, Promise promise) {
400415
CollectionContentRequestCallback collectionContentCallback = new CollectionContentRequestCallback() {
401416
@Override
402417
public void onComplete(@NotNull HelpCenterCollectionContent helpCenterCollectionContent) {
403-
promise.resolve(IntercomHelpCenterHelpers.parseHelpCenterCollectionsContentToReadableMap(helpCenterCollectionContent));
418+
promise.resolve(
419+
IntercomHelpCenterHelpers.parseHelpCenterCollectionsContentToReadableMap(helpCenterCollectionContent));
404420
}
405421

406422
@Override
@@ -435,7 +451,8 @@ public void searchHelpCenter(String searchTerm, Promise promise) {
435451
SearchRequestCallback collectionContentCallback = new SearchRequestCallback() {
436452
@Override
437453
public void onComplete(@NotNull List<HelpCenterArticleSearchResult> helpCenterArticleSearchResult) {
438-
promise.resolve(IntercomHelpCenterHelpers.parseHelpCenterArticleSearchToReadableArray(helpCenterArticleSearchResult));
454+
promise.resolve(
455+
IntercomHelpCenterHelpers.parseHelpCenterArticleSearchToReadableArray(helpCenterArticleSearchResult));
439456
}
440457

441458
@Override

0 commit comments

Comments
 (0)