Class AndroidNotificationCenter
Use the AndroidNotificationCenter to register notification channels and schedule local notifications.
Inherited Members
Namespace: Unity.Notifications.Android
Assembly: solution.dll
Syntax
public class AndroidNotificationCenter
Fields
SETTING_POST_NOTIFICATIONS_PERMISSION
A PlayerPrefs key used to save users reply to POST_NOTIFICATIONS request (integer value of the PermissionStatus). Value is one of PermissionStatus
Declaration
public static string SETTING_POST_NOTIFICATIONS_PERMISSION
Field Value
Type | Description |
---|---|
string |
Properties
IgnoringBatteryOptimizations
Whether app is ignoring device battery optimization settings. When device is in power saving or similar restricted mode, scheduled notifications may not appear or be late.
Declaration
public static bool IgnoringBatteryOptimizations { get; }
Property Value
Type | Description |
---|---|
bool |
See Also
ShouldShowPermissionToPostRationale
Returns true if app should show UI explaining why it need permission to post notifications. The UI should be shown before requesting the permission.
Declaration
public static bool ShouldShowPermissionToPostRationale { get; }
Property Value
Type | Description |
---|---|
bool |
UserPermissionToPost
Has user given permission to post notifications. Before Android 13 (API 33) no permission is required, but user can disable notifications in the Settings since Android 7 (API 24).
Declaration
public static PermissionStatus UserPermissionToPost { get; }
Property Value
Type | Description |
---|---|
PermissionStatus |
UsingExactScheduling
Whether notifications are scheduled at exact times. Combines notification settings and actual device settings (since Android 12 exact scheduling is user controllable).
Declaration
public static bool UsingExactScheduling { get; }
Property Value
Type | Description |
---|---|
bool |
See Also
Methods
CancelAllDisplayedNotifications()
Cancel all previously shown notifications. All notifications shown by the app will be removed from the status bar. All scheduled notifications will still be shown on their scheduled time.
Declaration
public static void CancelAllDisplayedNotifications()
CancelAllNotifications()
Cancel all notifications scheduled or previously shown by the app. All scheduled notifications will be canceled. All notifications shown by the app will be removed from the status bar.
Declaration
public static void CancelAllNotifications()
CancelAllScheduledNotifications()
Cancel all notifications scheduled by the app. All scheduled notifications will be canceled. Notifications will not be removed from the status bar if they are already shown.
Declaration
public static void CancelAllScheduledNotifications()
CancelDisplayedNotification(int)
Cancel a previously shown notification. The notification will be removed from the status bar.
Declaration
public static void CancelDisplayedNotification(int id)
Parameters
Type | Name | Description |
---|---|---|
int | id | ID of the notification to cancel |
CancelNotification(int)
Cancel a scheduled or previously shown notification. The notification will no longer be displayed on it's scheduled time. If it's already delivered it will be removed from the status bar.
Declaration
public static void CancelNotification(int id)
Parameters
Type | Name | Description |
---|---|---|
int | id | ID of the notification to cancel |
CancelScheduledNotification(int)
Cancel a scheduled notification. The notification will no longer be displayed on it's scheduled time. It it will not be removed from the status bar if it's already delivered.
Declaration
public static void CancelScheduledNotification(int id)
Parameters
Type | Name | Description |
---|---|---|
int | id | ID of the notification to cancel |
CheckScheduledNotificationStatus(int)
Return the status of a scheduled notification.
Declaration
public static NotificationStatus CheckScheduledNotificationStatus(int id)
Parameters
Type | Name | Description |
---|---|---|
int | id | ID of the notification to check |
Returns
Type | Description |
---|---|
NotificationStatus | The status of the notification |
CreateNotificationBuilder(int, AndroidNotification, string)
Create Notification.Builder object on Java side using privided AndroidNotification.
Declaration
public static AndroidJavaObject CreateNotificationBuilder(int id, AndroidNotification notification, string channelId)
Parameters
Type | Name | Description |
---|---|---|
int | id | ID for the notification |
AndroidNotification | notification | Struct with notification data |
string | channelId | Channel id |
Returns
Type | Description |
---|---|
AndroidJavaObject | A proxy object for created Notification.Builder |
CreateNotificationBuilder(AndroidNotification, string)
Create Notification.Builder. Will automatically generate the ID for notification. CreateNotificationBuilder(int, AndroidNotification, string)
Declaration
public static AndroidJavaObject CreateNotificationBuilder(AndroidNotification notification, string channelId)
Parameters
Type | Name | Description |
---|---|---|
AndroidNotification | notification | |
string | channelId |
Returns
Type | Description |
---|---|
AndroidJavaObject |
DeleteNotificationChannel(string)
Delete the specified notification channel.
Declaration
public static void DeleteNotificationChannel(string channelId)
Parameters
Type | Name | Description |
---|---|---|
string | channelId | ID of the channel to delete |
DeleteNotificationChannelGroup(string)
Delete notification channel group and all the channels in it.
Declaration
public static void DeleteNotificationChannelGroup(string id)
Parameters
Type | Name | Description |
---|---|---|
string | id | The ID of the group. |
GetLastNotificationIntent()
Allows retrieving the notification used to open the app. You can save arbitrary string data in the 'AndroidNotification.IntentData' field.
Declaration
public static AndroidNotificationIntentData GetLastNotificationIntent()
Returns
Type | Description |
---|---|
AndroidNotificationIntentData | Returns the AndroidNotification used to open the app, returns null if the app was not opened with a notification. |
GetNotificationChannel(string)
Returns the notification channel with the specified id. The notification channel struct fields might not be identical to the channel struct used to initially register the channel if they were changed by the user.
Declaration
public static AndroidNotificationChannel GetNotificationChannel(string channelId)
Parameters
Type | Name | Description |
---|---|---|
string | channelId | ID of the channel to retrieve |
Returns
Type | Description |
---|---|
AndroidNotificationChannel | Channel with given ID or empty struct if such channel does not exist |
GetNotificationChannels()
Returns all notification channels that were created by the app.
Declaration
public static AndroidNotificationChannel[] GetNotificationChannels()
Returns
Type | Description |
---|---|
AndroidNotificationChannel[] | All existing channels |
Initialize()
Initialize the AndroidNotificationCenter class. Can be safely called multiple times
Declaration
public static bool Initialize()
Returns
Type | Description |
---|---|
bool | True if has been successfully initialized |
OpenNotificationSettings(string)
Opens settings. On Android versions lower than 8.0 opens settings for the application. On Android 8.0 and later opens notification settings for the specified channel, or for the application, if channelId is null. Note, that opening settings will suspend the application and switch to settings app.
Declaration
public static void OpenNotificationSettings(string channelId = null)
Parameters
Type | Name | Description |
---|---|---|
string | channelId | ID for the channel to open or null to open notification settings for the application. |
RegisterNotificationChannel(AndroidNotificationChannel)
Creates a notification channel that notifications can be posted to. Notification channel settings can be changed by users on devices running Android 8.0 and above. On older Android versions settings set on the notification channel struct will still be applied to the notification if they are supported to by the Android version the app is running on.
Declaration
public static void RegisterNotificationChannel(AndroidNotificationChannel channel)
Parameters
Type | Name | Description |
---|---|---|
AndroidNotificationChannel | channel | Channel parameters |
Remarks
When a channel is deleted and recreated, all of the previous settings are restored. In order to change any settings besides the name or description an entirely new channel (with a different channel ID) must be created.
RegisterNotificationChannelGroup(AndroidNotificationChannelGroup)
Register notification channel group.
Declaration
public static void RegisterNotificationChannelGroup(AndroidNotificationChannelGroup group)
Parameters
Type | Name | Description |
---|---|---|
AndroidNotificationChannelGroup | group |
RequestExactScheduling()
Request user permission to schedule alarms at exact times. Only works on Android 12 and later, older versions can schedule at exact times without requesting it. This may cause your app to use more battery. App must have SCHEDULE_EXACT_ALARM permission to be able to request this.
Declaration
public static void RequestExactScheduling()
See Also
RequestIgnoreBatteryOptimizations()
Request user to allow unrestricted background work for app. UI for it is provided by OS and is manufacturer specific. Recommended to explain user what to do before requesting. App must have REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission to be able to request this.
Declaration
public static void RequestIgnoreBatteryOptimizations()
See Also
SendNotification(AndroidNotification, string)
Schedule a notification which will be shown at the time specified in the notification struct. The returned id can later be used to update the notification before it's triggered, it's current status can be tracked using CheckScheduledNotificationStatus.
Declaration
public static int SendNotification(AndroidNotification notification, string channelId)
Parameters
Type | Name | Description |
---|---|---|
AndroidNotification | notification | Data for the notification |
string | channelId | ID of the channel to send notification to |
Returns
Type | Description |
---|---|
int | The generated ID for the notification |
SendNotification(AndroidJavaObject)
Schedule a notification created using the provided Notification.Builder object. Notification builder should be created by calling CreateNotificationBuilder.
Declaration
public static void SendNotification(AndroidJavaObject notificationBuilder)
Parameters
Type | Name | Description |
---|---|---|
AndroidJavaObject | notificationBuilder |
SendNotification(AndroidJavaObject, out int)
Schedule a notification created using the provided Notification.Builder object. Notification builder should be created by calling CreateNotificationBuilder. Stores the notification id to the second argument
Declaration
public static void SendNotification(AndroidJavaObject notificationBuilder, out int id)
Parameters
Type | Name | Description |
---|---|---|
AndroidJavaObject | notificationBuilder | |
int | id |
SendNotificationWithExplicitID(AndroidNotification, string, int)
Schedule a notification which will be shown at the time specified in the notification struct. The specified id can later be used to update the notification before it's triggered, it's current status can be tracked using CheckScheduledNotificationStatus.
Declaration
public static void SendNotificationWithExplicitID(AndroidNotification notification, string channelId, int id)
Parameters
Type | Name | Description |
---|---|---|
AndroidNotification | notification | Data for the notification |
string | channelId | ID of the channel to send notification to |
int | id | A unique ID for the notification |
UpdateScheduledNotification(int, AndroidNotification, string)
Update an already scheduled notification. If a notification with the specified id was already scheduled it will be overridden with the information from the passed notification struct.
Declaration
public static void UpdateScheduledNotification(int id, AndroidNotification notification, string channelId)
Parameters
Type | Name | Description |
---|---|---|
int | id | ID of the notification to update |
AndroidNotification | notification | Data for the notification |
string | channelId | ID of the channel to send notification to |
Events
OnNotificationReceived
Subscribe to this event to receive callbacks whenever a scheduled notification is shown to the user.
Declaration
public static event AndroidNotificationCenter.NotificationReceivedCallback OnNotificationReceived
Event Type
Type | Description |
---|---|
AndroidNotificationCenter.NotificationReceivedCallback |