Skip to content

Commit c80746f

Browse files
committed
Added fallbackToSettings option to public API
Also added public comments to new promptForPushNotifications method. Cleaned up NotificationPermissionController permission string by moving it from a param to internal. Added Android API level to notification permission prompt check.
1 parent ee01f2c commit c80746f

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/model/MainActivityViewModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ private void setupSubscriptionSwitch() {
561561
subscriptionSwitch.setOnClickListener(v -> {
562562
OneSignal.disablePush(!subscriptionSwitch.isChecked());
563563
if (subscriptionSwitch.isChecked())
564-
OneSignal.promptForPushNotifications();
564+
OneSignal.promptForPushNotifications(true);
565565
});
566566
}
567567

OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationPermissionController.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
package com.onesignal
22

3+
import android.os.Build
4+
35
object NotificationPermissionController : PermissionsActivity.PermissionCallback {
46
private const val PERMISSION_TYPE = "NOTIFICATION"
7+
private const val ANDROID_PERMISSION_STRING = "android.permission.POST_NOTIFICATIONS"
58

69
init {
710
PermissionsActivity.registerAsCallback(PERMISSION_TYPE, this)
811
}
912

10-
fun prompt(
11-
fallbackToSettings: Boolean,
12-
androidPermissionString: String,
13-
) {
13+
fun prompt(fallbackToSettings: Boolean) {
14+
// TODO: Android 13 Beta 1 reports as 32 instead of 33, update to 33 once Google fixes this
15+
if (Build.VERSION.SDK_INT < 32)
16+
return
17+
1418
PermissionsActivity.startPrompt(
1519
fallbackToSettings,
1620
PERMISSION_TYPE,
17-
androidPermissionString,
21+
ANDROID_PERMISSION_STRING,
1822
this::class.java
1923
)
2024
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2838,8 +2838,25 @@ void onAnswered(OneSignal.PromptActionResult result) {
28382838
LocationController.getLocation(appContext, true, fallbackToSettings, locationHandler);
28392839
}
28402840

2841+
/**
2842+
* On Android 13 shows the system notification permission prompt to enable displaying
2843+
* notifications. This is required for apps that target Android API level 33 / "Tiramisu"
2844+
* to subscribe the device for push notifications.
2845+
*/
28412846
public static void promptForPushNotifications() {
2842-
NotificationPermissionController.INSTANCE.prompt(true, "android.permission.POST_NOTIFICATIONS");
2847+
promptForPushNotifications(false);
2848+
}
2849+
2850+
/**
2851+
* On Android 13 shows the system notification permission prompt to enable displaying
2852+
* notifications. This is required for apps that target Android API level 33 / "Tiramisu"
2853+
* to subscribe the device for push notifications.
2854+
*
2855+
* @param fallbackToSettings whether to show a Dialog to direct users to the App's notification
2856+
* settings if they have declined before.
2857+
*/
2858+
public static void promptForPushNotifications(boolean fallbackToSettings) {
2859+
NotificationPermissionController.INSTANCE.prompt(fallbackToSettings);
28432860
}
28442861

28452862
/**

0 commit comments

Comments
 (0)