Skip to content

Commit c88f87e

Browse files
committed
add notification permission response handler
Added new public method overload for promptForPushNotifications to accept a new optional PromptForPushNotificationPermissionResponseHandler type. This fires when the user accepts or declines the notification permission, includes handling of the fallbackToSettings logic as well.
1 parent 3ec1fe0 commit c88f87e

File tree

3 files changed

+85
-9
lines changed

3 files changed

+85
-9
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,17 @@ private void setupSubscriptionSwitch() {
560560

561561
subscriptionSwitch.setOnClickListener(v -> {
562562
OneSignal.disablePush(!subscriptionSwitch.isChecked());
563-
if (subscriptionSwitch.isChecked())
564-
OneSignal.promptForPushNotifications(true);
563+
if (subscriptionSwitch.isChecked()) {
564+
OneSignal.promptForPushNotifications(
565+
true,
566+
new OneSignal.PromptForPushNotificationPermissionResponseHandler() {
567+
@Override
568+
public void response(boolean accepted) {
569+
System.out.println("APP promptForPushNotifications:" + this + ":accepted: " + accepted);
570+
}
571+
}
572+
);
573+
}
565574
});
566575
}
567576

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

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,41 @@
2828
package com.onesignal
2929

3030
import android.os.Build
31+
import androidx.annotation.ChecksSdkIntAtLeast
3132

3233
object NotificationPermissionController : PermissionsActivity.PermissionCallback {
3334
private const val PERMISSION_TYPE = "NOTIFICATION"
3435
private const val ANDROID_PERMISSION_STRING = "android.permission.POST_NOTIFICATIONS"
3536

37+
private var callback: OneSignal.PromptForPushNotificationPermissionResponseHandler? = null
38+
3639
init {
3740
PermissionsActivity.registerAsCallback(PERMISSION_TYPE, this)
3841
}
3942

40-
fun prompt(fallbackToSettings: Boolean) {
41-
// TODO: Android 13 Beta 1 reports as 32 instead of 33, update to 33 once Google fixes this
42-
if (Build.VERSION.SDK_INT < 32)
43+
@ChecksSdkIntAtLeast(api = 33)
44+
val supportsNativePrompt =
45+
Build.VERSION.SDK_INT > 32 &&
46+
OSUtils.getTargetSdkVersion(OneSignal.appContext) > 32
47+
48+
fun prompt(
49+
fallbackToSettings: Boolean,
50+
callback: OneSignal.PromptForPushNotificationPermissionResponseHandler?,
51+
) {
52+
if (callback != null) this.callback = callback
53+
54+
if (notificationsEnabled()) {
55+
fireCallBack(true)
56+
return
57+
}
58+
59+
if (!supportsNativePrompt) {
60+
if (fallbackToSettings)
61+
showFallbackAlertDialog()
62+
else
63+
fireCallBack(false)
4364
return
65+
}
4466

4567
PermissionsActivity.startPrompt(
4668
fallbackToSettings,
@@ -52,14 +74,21 @@ object NotificationPermissionController : PermissionsActivity.PermissionCallback
5274

5375
override fun onAccept() {
5476
OneSignal.refreshNotificationPermissionState()
77+
fireCallBack(true)
5578
}
5679

5780
override fun onReject(fallbackToSettings: Boolean) {
58-
if (fallbackToSettings) showFallbackAlertDialog()
81+
val fallbackShown =
82+
if (fallbackToSettings)
83+
showFallbackAlertDialog()
84+
else
85+
false
86+
if (!fallbackShown) fireCallBack(false)
5987
}
6088

61-
private fun showFallbackAlertDialog() {
62-
val activity = OneSignal.getCurrentActivity() ?: return
89+
// Returns true if dialog was shown
90+
private fun showFallbackAlertDialog(): Boolean {
91+
val activity = OneSignal.getCurrentActivity() ?: return false
6392
AlertDialogPrepromptForAndroidSettings.show(
6493
activity,
6594
activity.getString(R.string.notification_permission_name_for_title),
@@ -69,8 +98,22 @@ object NotificationPermissionController : PermissionsActivity.PermissionCallback
6998
NavigateToAndroidSettingsForNotifications.show(activity)
7099
}
71100
override fun onDecline() {
101+
fireCallBack(false)
72102
}
73103
}
74104
)
105+
return true
75106
}
107+
108+
// Fires callback and clears it to ensure it is only called once.
109+
private fun fireCallBack(accepted: Boolean) {
110+
callback?.response(accepted)
111+
callback = null
112+
}
113+
114+
fun onAppForegrounded() {
115+
fireCallBack(notificationsEnabled())
116+
}
117+
118+
private fun notificationsEnabled() = OSUtils.areNotificationsEnabled(OneSignal.appContext)
76119
}

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,13 @@ public interface PostNotificationResponseHandler {
349349
void onFailure(JSONObject response);
350350
}
351351

352+
/**
353+
* Fires when the User accepts or declines the notification permission prompt.
354+
*/
355+
public interface PromptForPushNotificationPermissionResponseHandler {
356+
void response(boolean accepted);
357+
}
358+
352359
interface EntryStateListener {
353360
// Fire with the last appEntryState that just ended.
354361
void onEntryStateChange(AppEntryAction appEntryState);
@@ -1400,6 +1407,7 @@ static void onAppFocus() {
14001407
}
14011408

14021409
LocationController.onFocusChange();
1410+
NotificationPermissionController.INSTANCE.onAppForegrounded();
14031411

14041412
if (OSUtils.shouldLogMissingAppIdError(appId))
14051413
return;
@@ -2856,7 +2864,23 @@ public static void promptForPushNotifications() {
28562864
* settings if they have declined before.
28572865
*/
28582866
public static void promptForPushNotifications(boolean fallbackToSettings) {
2859-
NotificationPermissionController.INSTANCE.prompt(fallbackToSettings);
2867+
promptForPushNotifications(fallbackToSettings, null);
2868+
}
2869+
2870+
/**
2871+
* On Android 13 shows the system notification permission prompt to enable displaying
2872+
* notifications. This is required for apps that target Android API level 33 / "Tiramisu"
2873+
* to subscribe the device for push notifications.
2874+
*
2875+
* @param fallbackToSettings whether to show a Dialog to direct users to the App's notification
2876+
* settings if they have declined before.
2877+
* @param handler fires when the user declines ore accepts the notification permission prompt.
2878+
*/
2879+
public static void promptForPushNotifications(
2880+
boolean fallbackToSettings,
2881+
@Nullable PromptForPushNotificationPermissionResponseHandler handler
2882+
) {
2883+
NotificationPermissionController.INSTANCE.prompt(fallbackToSettings, handler);
28602884
}
28612885

28622886
/**

0 commit comments

Comments
 (0)