Skip to content

Commit ccc89b5

Browse files
authored
Merge pull request #1415 from OneSignal/add/android_notification_permission_prompting
[Feat] Add Android 13 push permission prompting
2 parents 28e20c6 + 0fd1a6b commit ccc89b5

File tree

8 files changed

+46
-28
lines changed

8 files changed

+46
-28
lines changed

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ dependencies {
2929

3030
// api is used instead of implementation so the parent :app project can access any of the OneSignal Java
3131
// classes if needed. Such as com.onesignal.NotificationExtenderService
32-
api 'com.onesignal:OneSignal:4.7.3'
32+
api 'com.onesignal:OneSignal:4.8.1'
3333

3434
testImplementation 'junit:junit:4.12'
3535
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,20 @@ public void onFailure(OneSignal.OSSMSUpdateError error) {
467467
});
468468
}
469469

470+
@ReactMethod
471+
public void promptForPushNotificationsWithUserResponse(final boolean fallbackToSettings, final Callback callback) {
472+
final Callback[] callbackArr = new Callback[]{ callback };
473+
OneSignal.promptForPushNotifications(fallbackToSettings, new OneSignal.PromptForPushNotificationPermissionResponseHandler() {
474+
@Override
475+
public void response(boolean accepted) {
476+
if (callbackArr[0] != null) {
477+
callbackArr[0].invoke(accepted);
478+
callbackArr[0] = null;
479+
}
480+
}
481+
});
482+
}
483+
470484
@ReactMethod
471485
public void promptLocation() {
472486
OneSignal.promptLocation();

examples/RNOneSignalTS/android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
android:label="@string/app_name"
1717
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
1818
android:launchMode="singleTask"
19+
android:exported="true"
1920
android:windowSoftInputMode="adjustResize">
2021
<intent-filter>
2122
<action android:name="android.intent.action.MAIN" />

examples/RNOneSignalTS/android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ buildscript {
44
ext {
55
buildToolsVersion = "29.0.2"
66
minSdkVersion = 21
7-
compileSdkVersion = 29
8-
targetSdkVersion = 29
7+
compileSdkVersion = 33
8+
targetSdkVersion = 33
99
}
1010
repositories {
1111
google()

examples/RNOneSignalTS/src/OSButtons.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class OSButtons extends React.Component<Props, State> {
7272
color,
7373
() => {
7474
loggingFunction("Prompting for push with user response...");
75-
OneSignal.promptForPushNotificationsWithUserResponse(response => {
75+
OneSignal.promptForPushNotificationsWithUserResponse(true, response => {
7676
loggingFunction(`User response: ${response}`);
7777
});
7878
}
@@ -115,13 +115,8 @@ class OSButtons extends React.Component<Props, State> {
115115

116116
elements.push(subscribedButton,
117117
unsubscribeWhenNotificationsAreDisabledButton,
118-
registerForProvisionalAuthorization,
118+
registerForProvisionalAuthorization, promptForPush,
119119
locationShared, setLocationShared, promptLocationButton);
120-
121-
if (Platform.OS === 'ios') {
122-
elements.push(promptForPush);
123-
}
124-
125120
return elements;
126121
}
127122

examples/RNOneSignalTS/src/OSDemo.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ class OSDemo extends React.Component<Props, State> {
3838
OneSignal.setAppId("ce8572ae-ff57-4e77-a265-5c91f00ecc4c");
3939
OneSignal.setLogLevel(6, 0);
4040
OneSignal.setRequiresUserPrivacyConsent(this.state.requiresPrivacyConsent);
41-
OneSignal.promptForPushNotificationsWithUserResponse(response => {
42-
this.OSLog("Prompt response:", response);
43-
});
4441

4542
/* O N E S I G N A L H A N D L E R S */
4643
OneSignal.setNotificationWillShowInForegroundHandler(notifReceivedEvent => {

ios/RCTOneSignal/RCTOneSignalEventEmitter.m

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,6 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body {
194194
[OneSignal setAppId:newAppId];
195195
}
196196

197-
RCT_EXPORT_METHOD(promptForPushNotificationPermissions:(RCTResponseSenderBlock)callback) {
198-
[OneSignal promptForPushNotificationsWithUserResponse:^(BOOL accepted) {
199-
callback(@[@(accepted)]);
200-
}];
201-
}
202-
203197
RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions) {
204198
if (RCTRunningInAppExtension()) {
205199
return;
@@ -326,11 +320,11 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body {
326320
}];
327321
}
328322

329-
RCT_EXPORT_METHOD(promptForPushNotificationsWithUserResponse:(RCTResponseSenderBlock)callback) {
323+
RCT_EXPORT_METHOD(promptForPushNotificationsWithUserResponse:(BOOL)fallbackToSettings withResponse:(RCTResponseSenderBlock)callback) {
330324
[OneSignal promptForPushNotificationsWithUserResponse:^(BOOL accepted) {
331325
[OneSignal onesignalLog:ONE_S_LL_VERBOSE message:@"Prompt For Push Notifications Success"];
332326
callback(@[@(accepted)]);
333-
}];
327+
} fallbackToSettings:fallbackToSettings];
334328
}
335329

336330
RCT_EXPORT_METHOD(registerForProvisionalAuthorization:(RCTResponseSenderBlock)callback) {

src/index.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,19 +170,36 @@ export default class OneSignal {
170170
/* R E G I S T R A T I O N E T C */
171171

172172
/**
173-
* Prompts the iOS user for push notifications.
173+
* Prompts the user for push notifications permission in iOS and Android 13+.
174+
* Use the fallbackToSettings parameter to prompt to open the settings app if a user has already declined push permissions.
175+
*
176+
* Call with promptForPushNotificationsWithUserResponse(fallbackToSettings?, handler?)
177+
*
178+
* Recommended: Do not use and instead follow: https://documentation.onesignal.com/docs/ios-push-opt-in-prompt.
179+
* @param {boolean} fallbackToSettings
174180
* @param {(response:boolean) => void} handler
175181
* @returns void
176182
*/
177-
static promptForPushNotificationsWithUserResponse(handler: (response: boolean) => void): void {
183+
static promptForPushNotificationsWithUserResponse(fallbackToSettingsOrHandler?: boolean | ((response: boolean) => void), handler?: (response: boolean) => void): void {
178184
if (!isNativeModuleLoaded(RNOneSignal)) return;
179185

180-
if (Platform.OS === 'ios') {
181-
isValidCallback(handler);
182-
RNOneSignal.promptForPushNotificationsWithUserResponse(handler);
183-
} else {
184-
console.log("promptForPushNotificationsWithUserResponse: this function is not supported on Android");
186+
let fallbackToSettings = false;
187+
188+
if (typeof fallbackToSettingsOrHandler === "function") {
189+
// Method was called like promptForPushNotificationsWithUserResponse(handler: function)
190+
handler = fallbackToSettingsOrHandler;
185191
}
192+
else if (typeof fallbackToSettingsOrHandler === "boolean") {
193+
// Method was called like promptForPushNotificationsWithUserResponse(fallbackToSettings: boolean, handler?: function)
194+
fallbackToSettings = fallbackToSettingsOrHandler;
195+
}
196+
// Else method was called like promptForPushNotificationsWithUserResponse(), no need to modify
197+
198+
if (!handler && Platform.OS === 'ios') {
199+
handler = function(){};
200+
}
201+
202+
RNOneSignal.promptForPushNotificationsWithUserResponse(fallbackToSettings, handler);
186203
}
187204

188205
/**

0 commit comments

Comments
 (0)