Skip to content

Commit 702e030

Browse files
author
Ali Abdelfattah
authored
Merge pull request #569 from Instabug/hotfix/android-push-notifications
[MOB-5157] Add new API: Replies.showNotificationAndroid
2 parents d3bf8fc + 1f7f5e6 commit 702e030

File tree

7 files changed

+107
-3
lines changed

7 files changed

+107
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
## v10.3.0 (2021-04-22)
22

3-
* Updates Instabug Android and iOS SDKs.
4-
* Migrates iOS to XCFramework.
3+
* Migrates iOS to XCFramework
4+
* Adds new API: Replies.showNotificationAndroid
55
* Deprecates Instabug.setVideoRecordingFloatingButtonPosition in favor of BugReporting.setVideoRecordingFloatingButtonPosition.
6+
* Updates Instabug Android and iOS SDKs
67

78
## v10.0.0 (2021-02-16)
89

__tests__/replies.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('Replies Module', () => {
2222
const setEnableInAppNotificationSound = sinon.spy(NativeModules.IBGReplies, 'setInAppNotificationSound');
2323
const setPushNotificationsEnabled = sinon.spy(NativeModules.IBGReplies, 'setPushNotificationsEnabled');
2424
const setPushNotificationRegistrationToken = sinon.spy(NativeModules.IBGReplies, 'setPushNotificationRegistrationToken');
25+
const showNotification = sinon.spy(NativeModules.IBGReplies, 'showNotification');
2526
const setNotificationIcon = sinon.spy(NativeModules.IBGReplies, 'setNotificationIcon');
2627
const setPushNotificationChannelId = sinon.spy(NativeModules.IBGReplies, 'setPushNotificationChannelId');
2728
const setSystemReplyNotificationSoundEnabled = sinon.spy(NativeModules.IBGReplies, 'setSystemReplyNotificationSoundEnabled');
@@ -32,6 +33,7 @@ describe('Replies Module', () => {
3233
IBGEventEmitter.removeAllListeners();
3334
setPushNotificationsEnabled.resetHistory();
3435
setPushNotificationRegistrationToken.resetHistory();
36+
showNotification.resetHistory();
3537
setNotificationIcon.resetHistory();
3638
setPushNotificationChannelId.resetHistory();
3739
setSystemReplyNotificationSoundEnabled.resetHistory();
@@ -140,6 +142,15 @@ describe('Replies Module', () => {
140142
expect(setPushNotificationRegistrationToken.calledOnceWithExactly('123')).toBe(true);
141143
});
142144

145+
146+
it('should call the native method showNotification on Android', () => {
147+
Platform.OS = 'android';
148+
Replies.showNotificationAndroid('test');
149+
150+
expect(showNotification.calledOnceWithExactly('test')).toBe(true);
151+
});
152+
153+
143154
it('should not call the native method setPushNotificationRegistrationToken on iOS', () => {
144155
Platform.OS = 'ios';
145156
Replies.setPushNotificationRegistrationTokenAndroid(true);

android/src/main/java/com/instabug/reactlibrary/RNInstabugRepliesModule.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77
import com.facebook.react.bridge.ReactApplicationContext;
88
import com.facebook.react.bridge.ReactContextBaseJavaModule;
99
import com.facebook.react.bridge.ReactMethod;
10+
import com.facebook.react.bridge.ReadableMap;
11+
import com.facebook.react.bridge.ReadableType;
12+
import com.facebook.react.bridge.ReadableMapKeySetIterator;
1013
import com.instabug.chat.Replies;
1114
import com.instabug.library.Feature;
1215
import com.instabug.reactlibrary.utils.InstabugUtil;
1316
import com.instabug.reactlibrary.utils.MainThreadHandler;
1417

1518
import javax.annotation.Nonnull;
19+
import java.util.HashMap;
20+
import java.util.Map;
1621

1722
public class RNInstabugRepliesModule extends ReactContextBaseJavaModule {
1823

@@ -169,6 +174,41 @@ public void run() {
169174
});
170175
}
171176

177+
/**
178+
* Show in-app Messaging's notifications
179+
*
180+
* @param data the data bundle related to Instabug
181+
*/
182+
@ReactMethod
183+
public void showNotification(final ReadableMap data) {
184+
MainThreadHandler.runOnMainThread(new Runnable() {
185+
@Override
186+
public void run() {
187+
try {
188+
Map<String, String> map = new HashMap<>();
189+
ReadableMapKeySetIterator iterator = data.keySetIterator();
190+
191+
while (iterator.hasNextKey()) {
192+
String key = iterator.nextKey();
193+
ReadableType type = data.getType(key);
194+
195+
switch(type) {
196+
case String:
197+
String value = data.getString(key);
198+
map.put(key, value);
199+
break;
200+
}
201+
}
202+
if (Replies.isInstabugNotification(map)) {
203+
Replies.showNotification(map);
204+
}
205+
} catch (Exception e) {
206+
e.printStackTrace();
207+
}
208+
}
209+
});
210+
}
211+
172212
/**
173213
* Set the push notification's icon that will be shown with Instabug notifications
174214
*

android/src/test/java/com/instabug/reactlibrary/RNInstabugRepliesModuleTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66

77
import com.facebook.react.bridge.Arguments;
88
import com.facebook.react.bridge.Callback;
9+
import com.facebook.react.bridge.JavaOnlyMap;
10+
import com.facebook.react.bridge.WritableMap;
911
import com.facebook.react.bridge.WritableNativeArray;
1012
import com.instabug.bug.BugReporting;
1113
import com.instabug.chat.Replies;
1214
import com.instabug.library.Feature;
1315
import com.instabug.reactlibrary.utils.InstabugUtil;
16+
import com.instabug.reactlibrary.utils.MapUtil;
1417
import com.instabug.survey.Surveys;
1518
import com.instabug.reactlibrary.utils.MainThreadHandler;
1619

@@ -27,6 +30,8 @@
2730

2831
import java.util.concurrent.Executors;
2932
import java.util.concurrent.ScheduledExecutorService;
33+
import java.util.Map;
34+
import java.util.HashMap;
3035

3136
import static org.mockito.Matchers.any;
3237
import static org.mockito.Matchers.anyLong;
@@ -170,6 +175,40 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable {
170175
Replies.setPushNotificationRegistrationToken("123");
171176
}
172177

178+
@Test
179+
public void givenBoolean$showNotification_whenQuery_thenShouldCallNativeApi() {
180+
// given
181+
PowerMockito.mockStatic(Replies.class);
182+
PowerMockito.mockStatic(SystemClock.class);
183+
PowerMockito.mockStatic(Arguments.class);
184+
185+
// when
186+
PowerMockito.when(Arguments.createMap())
187+
.thenAnswer(
188+
new Answer<Object>() {
189+
@Override
190+
public Object answer(InvocationOnMock invocation) throws Throwable {
191+
return new JavaOnlyMap();
192+
}
193+
});
194+
195+
Map<String, String> map = new HashMap<>();
196+
PowerMockito.when(Replies.isInstabugNotification(map))
197+
.thenAnswer(
198+
new Answer<Object>() {
199+
@Override
200+
public Object answer(InvocationOnMock invocation) throws Throwable {
201+
return true;
202+
}
203+
});
204+
205+
WritableMap readableMap = MapUtil.toWritableMap(new HashMap<String, Object>());
206+
rnModule.showNotification(readableMap);
207+
208+
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
209+
Replies.showNotification(map);
210+
}
211+
173212
@Test
174213
public void givenBoolean$setNotificationIcon_whenQuery_thenShouldCallNativeApi() {
175214
// given

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ export namespace Replies {
119119
): void;
120120
function setInAppNotificationSound(shouldPlaySound: boolean): void;
121121
function setPushNotificationRegistrationTokenAndroid(token: string): void;
122-
function setNotificationIconAndroid(notificationIcon: int): void;
122+
function showNotificationAndroid(data: object): void;
123+
function setNotificationIconAndroid(notificationIcon: number): void;
123124
function setPushNotificationChannelIdAndroid(pushNotificationChannelId: string): void;
124125
function setSystemReplyNotificationSoundEnabledAndroid(shouldPlaySound: boolean): void;
125126
}

jest/mockReplies.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jest.mock('NativeModules', () => {
1111
addListener: jest.fn(),
1212
setPushNotificationsEnabled: jest.fn(),
1313
setPushNotificationRegistrationToken: jest.fn(),
14+
showNotification: jest.fn(),
1415
setNotificationIcon: jest.fn(),
1516
setPushNotificationChannelId: jest.fn(),
1617
setSystemReplyNotificationSoundEnabled: jest.fn(),

modules/Replies.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ export default {
110110
}
111111
},
112112

113+
/**
114+
* Show in-app Messaging's notifications
115+
*
116+
* @param data the data bundle related to Instabug
117+
*/
118+
showNotificationAndroid(data) {
119+
if (Platform.OS === 'android') {
120+
IBGReplies.showNotification(data);
121+
}
122+
},
123+
113124
/**
114125
* Set the push notification's icon that will be shown with Instabug notifications
115126
*

0 commit comments

Comments
 (0)