Skip to content

Commit 1aa852b

Browse files
author
Ali Abdelfattah
authored
Merge pull request #549 from Instabug/feature/push-notifications
Feature/Push Notifications
2 parents 524dcab + 276f3c7 commit 1aa852b

File tree

7 files changed

+266
-16
lines changed

7 files changed

+266
-16
lines changed

__tests__/index.spec.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,6 @@ describe('Instabug Module', () => {
196196

197197
});
198198

199-
it('should not call the native method setPushNotificationsEnabled when platform is android', () => {
200-
201-
Platform.OS = 'android';
202-
Instabug.setPushNotificationsEnabled(true);
203-
204-
expect(setPushNotificationsEnabled.notCalled).toBe(true);
205-
206-
});
207-
208199
it('should call the native method setLocale', () => {
209200

210201
const locale = Instabug.locale.english;

__tests__/replies.spec.js

Lines changed: 61 additions & 4 deletions
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,24 +123,73 @@ describe('Replies Module', () => {
115123

116124
});
117125

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

120-
Platform.OS = 'ios';
121129
Replies.setPushNotificationsEnabled(true);
122130

123131
expect(setPushNotificationsEnabled.calledOnceWithExactly(true)).toBe(true);
124132

125133
});
126134

127135

128-
it('should not call the native method setPushNotificationsEnabled when platform is android', () => {
136+
it('should call the native method setPushNotificationRegistrationToken on Android', () => {
137+
Platform.OS = 'android';
138+
Replies.setPushNotificationRegistrationTokenAndroid('123');
139+
140+
expect(setPushNotificationRegistrationToken.calledOnceWithExactly('123')).toBe(true);
141+
});
142+
143+
it('should not call the native method setPushNotificationRegistrationToken on iOS', () => {
144+
Platform.OS = 'ios';
145+
Replies.setPushNotificationRegistrationTokenAndroid(true);
146+
147+
expect(setPushNotificationRegistrationToken.notCalled).toBe(true);
148+
});
149+
129150

151+
it('should call the native method setNotificationIcon on Android', () => {
130152
Platform.OS = 'android';
131-
Replies.setPushNotificationsEnabled(true);
153+
Replies.setNotificationIconAndroid(123);
154+
155+
expect(setNotificationIcon.calledOnceWithExactly(123)).toBe(true);
156+
});
132157

133-
expect(setPushNotificationsEnabled.notCalled).toBe(true);
158+
it('should not call the native method setNotificationIcon on iOS', () => {
159+
Platform.OS = 'ios';
160+
Replies.setNotificationIconAndroid(123);
134161

162+
expect(setNotificationIcon.notCalled).toBe(true);
135163
});
136164

137165

166+
it('should call the native method setPushNotificationChannelId on Android', () => {
167+
Platform.OS = 'android';
168+
Replies.setPushNotificationChannelIdAndroid('123');
169+
170+
expect(setPushNotificationChannelId.calledOnceWithExactly('123')).toBe(true);
171+
});
172+
173+
it('should not call the native method setPushNotificationChannelId on iOS', () => {
174+
Platform.OS = 'ios';
175+
Replies.setPushNotificationChannelIdAndroid('123');
176+
177+
expect(setPushNotificationChannelId.notCalled).toBe(true);
178+
});
179+
180+
181+
it('should call the native method setSystemReplyNotificationSoundEnabled on Android', () => {
182+
Platform.OS = 'android';
183+
Replies.setSystemReplyNotificationSoundEnabledAndroid(true);
184+
185+
expect(setSystemReplyNotificationSoundEnabled.calledOnceWithExactly(true)).toBe(true);
186+
});
187+
188+
it('should not call the native method setSystemReplyNotificationSoundEnabled on iOS', () => {
189+
Platform.OS = 'ios';
190+
Replies.setSystemReplyNotificationSoundEnabledAndroid(true);
191+
192+
expect(setSystemReplyNotificationSoundEnabled.notCalled).toBe(true);
193+
});
194+
138195
});

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,29 @@ public void run() {
108108
});
109109
}
110110

111+
/**
112+
* Enabled/disable push notifications
113+
*
114+
* @param isEnabled whether chat push notifications is enabled or not
115+
*/
116+
@ReactMethod
117+
public void setPushNotificationsEnabled(final boolean isEnabled) {
118+
MainThreadHandler.runOnMainThread(new Runnable() {
119+
@Override
120+
public void run() {
121+
try {
122+
if (isEnabled) {
123+
Replies.setPushNotificationState(Feature.State.ENABLED);
124+
} else {
125+
Replies.setPushNotificationState(Feature.State.DISABLED);
126+
}
127+
} catch (Exception e) {
128+
e.printStackTrace();
129+
}
130+
}
131+
});
132+
}
133+
111134
/**
112135
* Enabled/disable chat notification
113136
*
@@ -127,6 +150,85 @@ public void run() {
127150
});
128151
}
129152

153+
/**
154+
* Set the GCM registration token to Instabug
155+
*
156+
* @param token the GCM registration token
157+
*/
158+
@ReactMethod
159+
public void setPushNotificationRegistrationToken(final String token) {
160+
MainThreadHandler.runOnMainThread(new Runnable() {
161+
@Override
162+
public void run() {
163+
try {
164+
Replies.setPushNotificationRegistrationToken(token);
165+
} catch (Exception e) {
166+
e.printStackTrace();
167+
}
168+
}
169+
});
170+
}
171+
172+
/**
173+
* Set the push notification's icon that will be shown with Instabug notifications
174+
*
175+
* @param notificationIcon the notification icon resource ID
176+
*/
177+
@ReactMethod
178+
public void setNotificationIcon(final int notificationIcon) {
179+
MainThreadHandler.runOnMainThread(new Runnable() {
180+
@Override
181+
public void run() {
182+
try {
183+
Replies.setNotificationIcon(notificationIcon);
184+
} catch (Exception e) {
185+
e.printStackTrace();
186+
}
187+
}
188+
});
189+
}
190+
191+
192+
/**
193+
* Set a notification channel id to a notification channel that notifications
194+
* can be posted to.
195+
*
196+
* @param pushNotificationChannelId an id to a notification channel that notifications
197+
*/
198+
@ReactMethod
199+
public void setPushNotificationChannelId(final String pushNotificationChannelId) {
200+
MainThreadHandler.runOnMainThread(new Runnable() {
201+
@Override
202+
public void run() {
203+
try {
204+
Replies.setPushNotificationChannelId(pushNotificationChannelId);
205+
} catch (Exception e) {
206+
e.printStackTrace();
207+
}
208+
}
209+
});
210+
}
211+
212+
/**
213+
* Set whether new system notification received will play the default sound from
214+
* RingtoneManager or not (Default is {@code false})
215+
*
216+
* @param shouldPlaySound desired state of conversation sounds
217+
*/
218+
@ReactMethod
219+
public void setSystemReplyNotificationSoundEnabled(final boolean shouldPlaySound) {
220+
MainThreadHandler.runOnMainThread(new Runnable() {
221+
@Override
222+
public void run() {
223+
try {
224+
Replies.setSystemReplyNotificationSoundEnabled(shouldPlaySound);
225+
} catch (Exception e) {
226+
e.printStackTrace();
227+
}
228+
}
229+
});
230+
}
231+
130232
@ReactMethod
131233
public void setOnNewReplyReceivedHandler(final Callback onNewReplyReceivedCallback) {
132234
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,14 @@ export namespace Replies {
9494
function setInAppNotificationsEnabled(
9595
inAppNotificationsEnabled: boolean
9696
): void;
97+
function setPushNotificationsEnabled(
98+
isPushNotificationEnabled: boolean
99+
): void;
97100
function setInAppNotificationSound(shouldPlaySound: boolean): void;
101+
function setPushNotificationRegistrationTokenAndroid(token: string): void;
102+
function setNotificationIconAndroid(notificationIcon: int): void;
103+
function setPushNotificationChannelIdAndroid(pushNotificationChannelId: int): void;
104+
function setSystemReplyNotificationSoundEnabledAndroid(shouldPlaySound: int): void;
98105
}
99106
export namespace Surveys {
100107
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: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,52 @@ export default {
9696
* notifications are enabled or disabled.
9797
*/
9898
setPushNotificationsEnabled(isPushNotificationEnabled) {
99-
if (Platform.OS === 'ios')
100-
IBGReplies.setPushNotificationsEnabled(isPushNotificationEnabled);
99+
IBGReplies.setPushNotificationsEnabled(isPushNotificationEnabled);
100+
},
101+
102+
/**
103+
* Set the GCM registration token to Instabug
104+
*
105+
* @param token the GCM registration token
106+
*/
107+
setPushNotificationRegistrationTokenAndroid(token) {
108+
if (Platform.OS === 'android') {
109+
IBGReplies.setPushNotificationRegistrationToken(token);
110+
}
111+
},
112+
113+
/**
114+
* Set the push notification's icon that will be shown with Instabug notifications
115+
*
116+
* @param notificationIcon the notification icon resource ID
117+
*/
118+
setNotificationIconAndroid(notificationIcon) {
119+
if (Platform.OS === 'android') {
120+
IBGReplies.setNotificationIcon(notificationIcon);
121+
}
122+
},
123+
124+
/**
125+
* Set a notification channel id to a notification channel that notifications
126+
* can be posted to.
127+
*
128+
* @param pushNotificationChannelId an id to a notification channel that notifications
129+
*/
130+
setPushNotificationChannelIdAndroid(pushNotificationChannelId) {
131+
if (Platform.OS === 'android') {
132+
IBGReplies.setPushNotificationChannelId(pushNotificationChannelId);
133+
}
134+
},
135+
136+
/**
137+
* Set whether new system notification received will play the default sound from
138+
* RingtoneManager or not (Default is {@code false})
139+
*
140+
* @param shouldPlaySound desired state of conversation sounds
141+
*/
142+
setSystemReplyNotificationSoundEnabledAndroid(shouldPlaySound) {
143+
if (Platform.OS === 'android') {
144+
IBGReplies.setSystemReplyNotificationSoundEnabled(shouldPlaySound);
145+
}
101146
}
102147
};

0 commit comments

Comments
 (0)