Skip to content

Commit bbc00b3

Browse files
committed
Change permission request API to better match iOS
1 parent f68953f commit bbc00b3

File tree

3 files changed

+98
-63
lines changed

3 files changed

+98
-63
lines changed

com.unity.mobile.notifications/Runtime/Android/AndroidNotificationCenter.cs

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,6 @@
1414

1515
namespace Unity.Notifications.Android
1616
{
17-
/// <summary>
18-
/// Represents a status of the Android runtime permission.
19-
/// </summary>
20-
public enum PermissionStatus
21-
{
22-
/// <summary>
23-
/// No permission as user was not prompted for it.
24-
/// </summary>
25-
NotRequested = 0,
26-
27-
/// <summary>
28-
/// User gave permission.
29-
/// </summary>
30-
Allowed = 1,
31-
32-
/// <summary>
33-
/// User denied permission.
34-
/// </summary>
35-
Denied = 2,
36-
37-
/// <summary>
38-
/// User denied permission and expressed intent to not be prompted again.
39-
/// </summary>
40-
DeniedDontAskAgain = 3,
41-
42-
/// <summary>
43-
/// A request for permission was made and user hasn't responded yet.
44-
/// </summary>
45-
RequestPending = 4,
46-
}
47-
4817
/// <summary>
4918
/// Current status of a scheduled notification, can be queried using CheckScheduledNotificationStatus.
5019
/// </summary>
@@ -568,7 +537,7 @@ public static JniMethodID FindMethod(AndroidJavaClass clazz, string name, string
568537
public class AndroidNotificationCenter
569538
{
570539
private static int API_POST_NOTIFICATIONS_PERMISSION_REQUIRED = 33;
571-
private static string PERMISSION_POST_NOTIFICATIONS = "android.permission.POST_NOTIFICATIONS";
540+
internal static string PERMISSION_POST_NOTIFICATIONS = "android.permission.POST_NOTIFICATIONS";
572541

573542
/// <summary>
574543
/// A PlayerPrefs key used to save users reply to POST_NOTIFICATIONS request (integer value of the PermissionStatus).
@@ -627,7 +596,7 @@ public static bool Initialize()
627596
return s_Initialized;
628597
}
629598

630-
static void SetPostPermissionSetting(PermissionStatus status)
599+
internal static void SetPostPermissionSetting(PermissionStatus status)
631600
{
632601
PlayerPrefs.SetInt(SETTING_POST_NOTIFICATIONS_PERMISSION, (int)status);
633602
}
@@ -668,36 +637,6 @@ public static PermissionStatus UserPermissionToPost
668637
}
669638
}
670639

671-
/// <summary>
672-
/// Request user permission to post notifications.
673-
/// Before Android 13 (API 33) will allow immediately.
674-
/// May succeed or fail immediately. Users response is saved to PlayerPrefs.
675-
/// Respects users wish to not be asked again.
676-
/// </summary>
677-
/// <returns>PermissionStatus.RequestPending if user is prompted for permission or immediately known reply.</returns>
678-
/// <seealso cref="SETTING_POST_NOTIFICATIONS_PERMISSION"/>
679-
public static PermissionStatus RequestPermissionToPost()
680-
{
681-
var permissionStatus = UserPermissionToPost;
682-
if (permissionStatus == PermissionStatus.Allowed)
683-
return permissionStatus;
684-
if (permissionStatus == PermissionStatus.DeniedDontAskAgain)
685-
return permissionStatus;
686-
// Can only request permission if applications target SDK is 33, not actual device SDK
687-
if (s_TargetApiLevel < API_POST_NOTIFICATIONS_PERMISSION_REQUIRED)
688-
{
689-
// don't change setting here, in case app gets updated
690-
return PermissionStatus.DeniedDontAskAgain;
691-
}
692-
693-
var callbacks = new PermissionCallbacks();
694-
callbacks.PermissionGranted += (unused) => SetPostPermissionSetting(PermissionStatus.Allowed);
695-
callbacks.PermissionDenied += (unused) => SetPostPermissionSetting(PermissionStatus.Denied);
696-
callbacks.PermissionDeniedAndDontAskAgain += (unused) => SetPostPermissionSetting(PermissionStatus.DeniedDontAskAgain);
697-
Permission.RequestUserPermission(PERMISSION_POST_NOTIFICATIONS, callbacks);
698-
return PermissionStatus.RequestPending;
699-
}
700-
701640
/// <summary>
702641
/// Creates a notification channel that notifications can be posted to.
703642
/// Notification channel settings can be changed by users on devices running Android 8.0 and above.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using UnityEngine.Android;
2+
3+
namespace Unity.Notifications.Android
4+
{
5+
/// <summary>
6+
/// Represents a status of the Android runtime permission.
7+
/// </summary>
8+
public enum PermissionStatus
9+
{
10+
/// <summary>
11+
/// No permission as user was not prompted for it.
12+
/// </summary>
13+
NotRequested = 0,
14+
15+
/// <summary>
16+
/// User gave permission.
17+
/// </summary>
18+
Allowed = 1,
19+
20+
/// <summary>
21+
/// User denied permission.
22+
/// </summary>
23+
Denied = 2,
24+
25+
/// <summary>
26+
/// User denied permission and expressed intent to not be prompted again.
27+
/// </summary>
28+
DeniedDontAskAgain = 3,
29+
30+
/// <summary>
31+
/// A request for permission was made and user hasn't responded yet.
32+
/// </summary>
33+
RequestPending = 4,
34+
}
35+
36+
/// <summary>
37+
/// A class to request permission to post notifications.
38+
/// Before Android 13 (API 33) it is not required and Status will become Allowed immediately.
39+
/// May succeed or fail immediately. Users response is saved to PlayerPrefs.
40+
/// Respects users wish to not be asked again.
41+
/// </summary>
42+
/// <seealso cref="AndroidNotificationCenter.UserPermissionToPost"/>
43+
/// <seealso cref="AndroidNotificationCenter.SETTING_POST_NOTIFICATIONS_PERMISSION"/>
44+
public class PermissionRequest
45+
{
46+
/// <summary>
47+
/// The status of this request.
48+
/// Value other than RequestPending means request has completed.
49+
/// </summary>
50+
public PermissionStatus Status { get; set; }
51+
52+
/// <summary>
53+
/// Create a new request.
54+
/// Will show user a dialog asking for permission if that is required to post notifications and user hasn't permanently denied it already.
55+
/// </summary>
56+
/// <see cref="PermissionStatus.DeniedDontAskAgain"/>
57+
public PermissionRequest()
58+
{
59+
Status = AndroidNotificationCenter.UserPermissionToPost;
60+
switch (Status)
61+
{
62+
case PermissionStatus.NotRequested:
63+
case PermissionStatus.Denied:
64+
Status = PermissionStatus.RequestPending;
65+
RequestPermission();
66+
break;
67+
}
68+
}
69+
70+
void RequestPermission()
71+
{
72+
var callbacks = new PermissionCallbacks();
73+
callbacks.PermissionGranted += (unused) => PermissionResponse(PermissionStatus.Allowed);
74+
callbacks.PermissionDenied += (unused) => PermissionResponse(PermissionStatus.Denied);
75+
callbacks.PermissionDeniedAndDontAskAgain += (unused) => PermissionResponse(PermissionStatus.DeniedDontAskAgain);
76+
Permission.RequestUserPermission(AndroidNotificationCenter.PERMISSION_POST_NOTIFICATIONS, callbacks);
77+
}
78+
79+
void PermissionResponse(PermissionStatus status)
80+
{
81+
Status = status;
82+
AndroidNotificationCenter.SetPostPermissionSetting(status);
83+
}
84+
}
85+
}

com.unity.mobile.notifications/Runtime/Android/NotificationPermission.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)