@@ -24,18 +24,62 @@ package com.leanplum.utils
2424import android.Manifest
2525import android.annotation.TargetApi
2626import android.app.Activity
27+ import android.content.Context
2728import android.content.pm.PackageManager
2829import androidx.core.app.ActivityCompat
2930import androidx.core.app.NotificationManagerCompat
3031import androidx.core.content.ContextCompat
32+ import com.leanplum.Leanplum
3133import 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 */
3741var 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 )
4084private fun isNotificationPermissionGranted (activity : Activity ): Boolean {
4185 val res = ContextCompat .checkSelfPermission(activity, Manifest .permission.POST_NOTIFICATIONS )
@@ -53,13 +97,17 @@ fun shouldShowRegisterForPush(activity: Activity): Boolean {
5397fun 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 )
59104fun 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
65113fun 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