Skip to content

Commit 0a4ce2d

Browse files
committed
Add unsubscribeWhenNotificationsAreDisabled method support
* Add unsubscribeWhenNotificationsAreDisabled() Android support
1 parent 5c31c85 commit 0a4ce2d

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

android/src/main/java/com/geektime/rnonesignalandroid/RNOneSignal.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ public void disablePush(boolean disable) {
259259
OneSignal.disablePush(disable);
260260
}
261261

262+
@ReactMethod
263+
public void unsubscribeWhenNotificationsAreDisabled(boolean unsubscribe) {
264+
OneSignal.unsubscribeWhenNotificationsAreDisabled(unsubscribe);
265+
}
266+
262267
@ReactMethod
263268
public void sendTag(String key, String value) {
264269
OneSignal.sendTag(key, value);

examples/RNOneSignalTS/src/OSButtons.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface Props {
1212

1313
export interface State {
1414
isSubscribed: boolean;
15+
unSubscribedWhenNotificationDisabled: boolean;
1516
isLocationShared: boolean;
1617
provideUserConsent: boolean;
1718
requireUserConsent: boolean;
@@ -26,6 +27,7 @@ class OSButtons extends React.Component<Props, State> {
2627

2728
this.state = {
2829
isSubscribed: subscribeFields.isSubscribed,
30+
unSubscribedWhenNotificationDisabled: true,
2931
isLocationShared: true,
3032
provideUserConsent: false,
3133
requireUserConsent: false,
@@ -42,7 +44,7 @@ class OSButtons extends React.Component<Props, State> {
4244
createSubscribeFields() {
4345
const { subscribeFields, loggingFunction } = this.props;
4446
const { isSubscribed } = subscribeFields;
45-
const { isLocationShared } = this.state;
47+
const { unSubscribedWhenNotificationDisabled, isLocationShared } = this.state;
4648
const color = '#D45653';
4749
const elements = [];
4850

@@ -54,6 +56,16 @@ class OSButtons extends React.Component<Props, State> {
5456
OneSignal.disablePush(isSubscribed);
5557
}
5658
);
59+
60+
const unsubscribeWhenNotificationsAreDisabledButton = renderButtonView(
61+
unSubscribedWhenNotificationDisabled ? "Unsubscribe When Notifications Disabled" : "Subscribe when notification disabled",
62+
color,
63+
() => {
64+
loggingFunction(`Is application unsubscribed when notification disabled: ${unSubscribedWhenNotificationDisabled}`);
65+
OneSignal.unsubscribeWhenNotificationsAreDisabled(unSubscribedWhenNotificationDisabled);
66+
this.setState({ unSubscribedWhenNotificationDisabled : !unSubscribedWhenNotificationDisabled });
67+
}
68+
);
5769

5870
const promptForPush = renderButtonView(
5971
"Prompt for Push",
@@ -90,7 +102,7 @@ class OSButtons extends React.Component<Props, State> {
90102
}
91103
);
92104

93-
elements.push(subscribedButton, locationShared, setLocationShared, promptLocationButton);
105+
elements.push(subscribedButton, unsubscribeWhenNotificationsAreDisabledButton, locationShared, setLocationShared, promptLocationButton);
94106

95107
if (Platform.OS === 'ios') {
96108
elements.push(promptForPush);

src/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ declare module 'react-native-onesignal' {
188188
*/
189189
disablePush(disable: boolean): void;
190190

191+
/**
192+
* Android Only. If notifications are disabled for your application, unsubscribe the user from OneSignal.
193+
* @param {boolean} unsubscribe
194+
* @returns void
195+
*/
196+
unsubscribeWhenNotificationsAreDisabled(unsubscribe: boolean): void;
197+
191198
/**
192199
* True if the application has location share activated, false otherwise
193200
* @returns Promise<boolean>

src/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ export default class OneSignal {
100100
RNOneSignal.disablePush(disable);
101101
}
102102

103+
static unsubscribeWhenNotificationsAreDisabled(unsubscribe) {
104+
if (!checkIfInitialized(RNOneSignal)) return;
105+
106+
if (Platform.OS === 'android') {
107+
RNOneSignal.unsubscribeWhenNotificationsAreDisabled(unsubscribe);
108+
} else {
109+
console.log("unsubscribeWhenNotificationsAreDisabled: this function is not supported on iOS");
110+
}
111+
}
112+
103113
/* L O C A T I O N */
104114

105115
static isLocationShared() {

0 commit comments

Comments
 (0)