Skip to content

Commit d241867

Browse files
Map Push Notifications Android APIs
1 parent bb7aefd commit d241867

File tree

6 files changed

+246
-2
lines changed

6 files changed

+246
-2
lines changed

__tests__/replies.spec.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,20 @@ describe('Replies Module', () => {
2121
const setChatNotificationEnabled = sinon.spy(NativeModules.IBGReplies, 'setInAppNotificationEnabled');
2222
const setEnableInAppNotificationSound = sinon.spy(NativeModules.IBGReplies, 'setInAppNotificationSound');
2323
const setPushNotificationsEnabled = sinon.spy(NativeModules.IBGReplies, 'setPushNotificationsEnabled');
24+
const setPushNotificationRegistrationToken = sinon.spy(NativeModules.IBGReplies, 'setPushNotificationRegistrationToken');
25+
const setNotificationIcon = sinon.spy(NativeModules.IBGReplies, 'setNotificationIcon');
26+
const setPushNotificationChannelId = sinon.spy(NativeModules.IBGReplies, 'setPushNotificationChannelId');
27+
const setSystemReplyNotificationSoundEnabled = sinon.spy(NativeModules.IBGReplies, 'setSystemReplyNotificationSoundEnabled');
2428

2529
beforeEach(() => {
2630
setOnNewReplyReceivedCallback.resetHistory();
2731
setEnableInAppNotificationSound.resetHistory();
2832
IBGEventEmitter.removeAllListeners();
2933
setPushNotificationsEnabled.resetHistory();
34+
setPushNotificationRegistrationToken.resetHistory();
35+
setNotificationIcon.resetHistory();
36+
setPushNotificationChannelId.resetHistory();
37+
setSystemReplyNotificationSoundEnabled.resetHistory();
3038
});
3139

3240
it('should call the native method setRepliesEnabled', () => {
@@ -115,6 +123,7 @@ describe('Replies Module', () => {
115123

116124
});
117125

126+
118127
it('should call the native method setPushNotificationsEnabled', () => {
119128

120129
Platform.OS = 'ios';
@@ -124,7 +133,6 @@ describe('Replies Module', () => {
124133

125134
});
126135

127-
128136
it('should not call the native method setPushNotificationsEnabled when platform is android', () => {
129137

130138
Platform.OS = 'android';
@@ -135,4 +143,63 @@ describe('Replies Module', () => {
135143
});
136144

137145

146+
it('should call the native method setPushNotificationRegistrationToken on Android', () => {
147+
Platform.OS = 'android';
148+
Replies.setPushNotificationRegistrationTokenAndroid('123');
149+
150+
expect(setPushNotificationRegistrationToken.calledOnceWithExactly('123')).toBe(true);
151+
});
152+
153+
it('should not call the native method setPushNotificationRegistrationToken on iOS', () => {
154+
Platform.OS = 'ios';
155+
Replies.setPushNotificationRegistrationTokenAndroid(true);
156+
157+
expect(setPushNotificationRegistrationToken.notCalled).toBe(true);
158+
});
159+
160+
161+
it('should call the native method setNotificationIcon on Android', () => {
162+
Platform.OS = 'android';
163+
Replies.setNotificationIconAndroid(123);
164+
165+
expect(setNotificationIcon.calledOnceWithExactly(123)).toBe(true);
166+
});
167+
168+
it('should not call the native method setNotificationIcon on iOS', () => {
169+
Platform.OS = 'ios';
170+
Replies.setNotificationIconAndroid(123);
171+
172+
expect(setNotificationIcon.notCalled).toBe(true);
173+
});
174+
175+
176+
it('should call the native method setPushNotificationChannelId on Android', () => {
177+
Platform.OS = 'android';
178+
Replies.setPushNotificationChannelIdAndroid('123');
179+
180+
expect(setPushNotificationChannelId.calledOnceWithExactly('123')).toBe(true);
181+
});
182+
183+
it('should not call the native method setPushNotificationChannelId on iOS', () => {
184+
Platform.OS = 'ios';
185+
Replies.setPushNotificationChannelIdAndroid('123');
186+
187+
expect(setPushNotificationChannelId.notCalled).toBe(true);
188+
});
189+
190+
191+
it('should call the native method setSystemReplyNotificationSoundEnabled on Android', () => {
192+
Platform.OS = 'android';
193+
Replies.setSystemReplyNotificationSoundEnabledAndroid(true);
194+
195+
expect(setSystemReplyNotificationSoundEnabled.calledOnceWithExactly(true)).toBe(true);
196+
});
197+
198+
it('should not call the native method setSystemReplyNotificationSoundEnabled on iOS', () => {
199+
Platform.OS = 'ios';
200+
Replies.setSystemReplyNotificationSoundEnabledAndroid(true);
201+
202+
expect(setSystemReplyNotificationSoundEnabled.notCalled).toBe(true);
203+
});
204+
138205
});

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,85 @@ public void run() {
127127
});
128128
}
129129

130+
/**
131+
* Set the GCM registration token to Instabug
132+
*
133+
* @param token the GCM registration token
134+
*/
135+
@ReactMethod
136+
public void setPushNotificationRegistrationToken(final String token) {
137+
MainThreadHandler.runOnMainThread(new Runnable() {
138+
@Override
139+
public void run() {
140+
try {
141+
Replies.setPushNotificationRegistrationToken(token);
142+
} catch (Exception e) {
143+
e.printStackTrace();
144+
}
145+
}
146+
});
147+
}
148+
149+
/**
150+
* Set the push notification's icon that will be shown with Instabug notifications
151+
*
152+
* @param notificationIcon the notification icon resource ID
153+
*/
154+
@ReactMethod
155+
public void setNotificationIcon(final int notificationIcon) {
156+
MainThreadHandler.runOnMainThread(new Runnable() {
157+
@Override
158+
public void run() {
159+
try {
160+
Replies.setNotificationIcon(notificationIcon);
161+
} catch (Exception e) {
162+
e.printStackTrace();
163+
}
164+
}
165+
});
166+
}
167+
168+
169+
/**
170+
* Set a notification channel id to a notification channel that notifications
171+
* can be posted to.
172+
*
173+
* @param pushNotificationChannelId an id to a notification channel that notifications
174+
*/
175+
@ReactMethod
176+
public void setPushNotificationChannelId(final String pushNotificationChannelId) {
177+
MainThreadHandler.runOnMainThread(new Runnable() {
178+
@Override
179+
public void run() {
180+
try {
181+
Replies.setPushNotificationChannelId(pushNotificationChannelId);
182+
} catch (Exception e) {
183+
e.printStackTrace();
184+
}
185+
}
186+
});
187+
}
188+
189+
/**
190+
* Set whether new system notification received will play the default sound from
191+
* RingtoneManager or not (Default is {@code false})
192+
*
193+
* @param shouldPlaySound desired state of conversation sounds
194+
*/
195+
@ReactMethod
196+
public void setSystemReplyNotificationSoundEnabled(final boolean shouldPlaySound) {
197+
MainThreadHandler.runOnMainThread(new Runnable() {
198+
@Override
199+
public void run() {
200+
try {
201+
Replies.setSystemReplyNotificationSoundEnabled(shouldPlaySound);
202+
} catch (Exception e) {
203+
e.printStackTrace();
204+
}
205+
}
206+
});
207+
}
208+
130209
@ReactMethod
131210
public void setOnNewReplyReceivedHandler(final Callback onNewReplyReceivedCallback) {
132211
MainThreadHandler.runOnMainThread(new Runnable() {

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,48 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable {
159159
Replies.setInAppNotificationSound(true);
160160
}
161161

162+
@Test
163+
public void givenBoolean$setPushNotificationRegistrationToken_whenQuery_thenShouldCallNativeApi() {
164+
// given
165+
PowerMockito.mockStatic(Replies.class);
166+
// when
167+
rnModule.setPushNotificationRegistrationToken("123");
168+
// then
169+
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
170+
Replies.setPushNotificationRegistrationToken("123");
171+
}
172+
173+
@Test
174+
public void givenBoolean$setNotificationIcon_whenQuery_thenShouldCallNativeApi() {
175+
// given
176+
PowerMockito.mockStatic(Replies.class);
177+
// when
178+
rnModule.setNotificationIcon(123);
179+
// then
180+
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
181+
Replies.setNotificationIcon(123);
182+
}
183+
184+
@Test
185+
public void givenBoolean$setPushNotificationChannelId_whenQuery_thenShouldCallNativeApi() {
186+
// given
187+
PowerMockito.mockStatic(Replies.class);
188+
// when
189+
rnModule.setPushNotificationChannelId("123");
190+
// then
191+
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
192+
Replies.setPushNotificationChannelId("123");
193+
}
194+
195+
@Test
196+
public void givenBoolean$setSystemReplyNotificationSoundEnabled_whenQuery_thenShouldCallNativeApi() {
197+
// given
198+
PowerMockito.mockStatic(Replies.class);
199+
// when
200+
rnModule.setSystemReplyNotificationSoundEnabled(true);
201+
// then
202+
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
203+
Replies.setSystemReplyNotificationSoundEnabled(true);
204+
}
205+
162206
}

index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ export namespace Replies {
9595
inAppNotificationsEnabled: boolean
9696
): void;
9797
function setInAppNotificationSound(shouldPlaySound: boolean): void;
98+
function setPushNotificationRegistrationTokenAndroid(token: string): void;
99+
function setNotificationIconAndroid(notificationIcon: int): void;
100+
function setPushNotificationChannelIdAndroid(pushNotificationChannelId: int): void;
101+
function setSystemReplyNotificationSoundEnabledAndroid(shouldPlaySound: int): void;
98102
}
99103
export namespace Surveys {
100104
function setEnabled(isEnabled: boolean): void;

jest/mockReplies.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ jest.mock('NativeModules', () => {
99
setInAppNotificationEnabled: jest.fn(),
1010
setInAppNotificationSound: jest.fn(),
1111
addListener: jest.fn(),
12-
setPushNotificationsEnabled: jest.fn()
12+
setPushNotificationsEnabled: jest.fn(),
13+
setPushNotificationRegistrationToken: jest.fn(),
14+
setNotificationIcon: jest.fn(),
15+
setPushNotificationChannelId: jest.fn(),
16+
setSystemReplyNotificationSoundEnabled: jest.fn(),
1317
},
1418
};
1519
});

modules/Replies.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,51 @@ export default {
9898
setPushNotificationsEnabled(isPushNotificationEnabled) {
9999
if (Platform.OS === 'ios')
100100
IBGReplies.setPushNotificationsEnabled(isPushNotificationEnabled);
101+
},
102+
103+
/**
104+
* Set the GCM registration token to Instabug
105+
*
106+
* @param token the GCM registration token
107+
*/
108+
setPushNotificationRegistrationTokenAndroid(token) {
109+
if (Platform.OS === 'android') {
110+
IBGReplies.setPushNotificationRegistrationToken(token);
111+
}
112+
},
113+
114+
/**
115+
* Set the push notification's icon that will be shown with Instabug notifications
116+
*
117+
* @param notificationIcon the notification icon resource ID
118+
*/
119+
setNotificationIconAndroid(notificationIcon) {
120+
if (Platform.OS === 'android') {
121+
IBGReplies.setNotificationIcon(notificationIcon);
122+
}
123+
},
124+
125+
/**
126+
* Set a notification channel id to a notification channel that notifications
127+
* can be posted to.
128+
*
129+
* @param pushNotificationChannelId an id to a notification channel that notifications
130+
*/
131+
setPushNotificationChannelIdAndroid(pushNotificationChannelId) {
132+
if (Platform.OS === 'android') {
133+
IBGReplies.setPushNotificationChannelId(pushNotificationChannelId);
134+
}
135+
},
136+
137+
/**
138+
* Set whether new system notification received will play the default sound from
139+
* RingtoneManager or not (Default is {@code false})
140+
*
141+
* @param shouldPlaySound desired state of conversation sounds
142+
*/
143+
setSystemReplyNotificationSoundEnabledAndroid(shouldPlaySound) {
144+
if (Platform.OS === 'android') {
145+
IBGReplies.setSystemReplyNotificationSoundEnabled(shouldPlaySound);
146+
}
101147
}
102148
};

0 commit comments

Comments
 (0)