Skip to content

Commit db9ca23

Browse files
authored
Enable user for onRequestPermissionResult feedback (#544)
1 parent d4f4870 commit db9ca23

File tree

2 files changed

+96
-5
lines changed

2 files changed

+96
-5
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2023, Leanplum, Inc. All rights reserved.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package com.leanplum.utils
23+
24+
import com.leanplum.Leanplum
25+
import kotlin.properties.ReadWriteProperty
26+
import kotlin.reflect.KProperty
27+
28+
internal class IntPreference(
29+
private val key: String,
30+
private val defaultValue: Int
31+
) : ReadWriteProperty<Nothing?, Int> {
32+
33+
override fun getValue(thisRef: Nothing?, property: KProperty<*>): Int {
34+
val prefs = Leanplum.getContext()?.getLeanplumPrefs() ?: return defaultValue
35+
return prefs.getInt(key, defaultValue)
36+
}
37+
38+
override fun setValue(thisRef: Nothing?, property: KProperty<*>, value: Int) {
39+
val prefs = Leanplum.getContext()?.getLeanplumPrefs() ?: return
40+
prefs.edit().putInt(key, value).apply()
41+
}
42+
}

AndroidSDKCore/src/main/java/com/leanplum/utils/PushPermissionUtil.kt

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,62 @@ package com.leanplum.utils
2424
import android.Manifest
2525
import android.annotation.TargetApi
2626
import android.app.Activity
27+
import android.content.Context
2728
import android.content.pm.PackageManager
2829
import androidx.core.app.ActivityCompat
2930
import androidx.core.app.NotificationManagerCompat
3031
import androidx.core.content.ContextCompat
32+
import com.leanplum.Leanplum
3133
import com.leanplum.internal.Log
3234

35+
private const val DECLINE_LIMIT = 2
36+
3337
/**
3438
* Could be changed by client if code is already in use. Request code can be used in activity's
35-
* onRequestPermissionsResult method to receive feedback whether the permission was granted or not.
39+
* [onRequestPermissionResult] method to receive feedback whether the permission was granted or not.
3640
*/
3741
var pushPermissionRequestCode = 1233321
3842

43+
/**
44+
* Decline count is tracked only when [onRequestPermissionResult] is invoked by client.
45+
*
46+
* Setting value of 2 would disable asking for push permission permanently.
47+
* Setting value of 0 would ask for push permission again.
48+
*/
49+
var declineCount: Int by IntPreference(key = "push_permission_decline_count", defaultValue = 0)
50+
51+
/**
52+
* Invoke method from your activity's onRequestPermissionResult to allow Leanplum SDK to track the
53+
* number of consecutive declines of the POST_NOTIFICATIONS permission. When two consecutive
54+
* declines happen none of the permission dialogs will be shown again and user would have to
55+
* manually allow notifications. Note that dismissing the native dialog without clicking on Allow or
56+
* Don't Allow does count as a decline from OS API.
57+
*/
58+
fun onRequestPermissionResult(requestCode: Int,
59+
permissions: Array<String>,
60+
grantResults: IntArray
61+
) {
62+
val context: Context? = Leanplum.getContext()
63+
if (context == null || !BuildUtil.isPushPermissionSupported(context)) {
64+
return
65+
}
66+
67+
if (requestCode != pushPermissionRequestCode || permissions.size != grantResults.size) {
68+
return
69+
}
70+
71+
for (i in permissions.indices) {
72+
if (permissions[i] == Manifest.permission.POST_NOTIFICATIONS) {
73+
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
74+
declineCount = 0
75+
} else {
76+
declineCount++
77+
}
78+
break
79+
}
80+
}
81+
}
82+
3983
@TargetApi(33)
4084
private fun isNotificationPermissionGranted(activity: Activity): Boolean {
4185
val res = ContextCompat.checkSelfPermission(activity, Manifest.permission.POST_NOTIFICATIONS)
@@ -53,13 +97,17 @@ fun shouldShowRegisterForPush(activity: Activity): Boolean {
5397
fun shouldShowPrePermission(activity: Activity): Boolean {
5498
return shouldShowRegisterForPush(activity)
5599
&& ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.POST_NOTIFICATIONS)
100+
&& declineCount < DECLINE_LIMIT
56101
}
57102

58103
@TargetApi(33)
59104
fun requestNativePermission(activity: Activity) {
60-
activity.requestPermissions(
61-
arrayOf(Manifest.permission.POST_NOTIFICATIONS),
62-
pushPermissionRequestCode)
105+
if (declineCount < DECLINE_LIMIT) {
106+
activity.requestPermissions(
107+
arrayOf(Manifest.permission.POST_NOTIFICATIONS),
108+
pushPermissionRequestCode
109+
)
110+
}
63111
}
64112

65113
fun printDebugLog(activity: Activity) {
@@ -73,7 +121,8 @@ fun printDebugLog(activity: Activity) {
73121

74122
Log.d("Notification permission: granted=$permissionGranted " +
75123
"notificationsEnabled=$notificationsEnabled " +
76-
"shouldShowRequestPermissionRationale=$shouldShowRequestPermissionRationale")
124+
"shouldShowRequestPermissionRationale=$shouldShowRequestPermissionRationale " +
125+
"declineCount=$declineCount")
77126
} else {
78127
Log.d("Notification permission: not supported by target or device version")
79128
}

0 commit comments

Comments
 (0)