Class AndroidNotificationCenter
Use the AndroidNotificationCenter to register notification channels and schedule local notifications.
Namespace: Unity.Notifications.Android
Syntax
public class AndroidNotificationCenter
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(Int32)
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 |
---|---|---|
Int32 | id | ID of the notification to cancel |
CancelNotification(Int32)
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 |
---|---|---|
Int32 | id | ID of the notification to cancel |
CancelScheduledNotification(Int32)
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 |
---|---|---|
Int32 | id | ID of the notification to cancel |
CheckScheduledNotificationStatus(Int32)
Return the status of a scheduled notification.
Declaration
public static NotificationStatus CheckScheduledNotificationStatus(int id)
Parameters
Type | Name | Description |
---|---|---|
Int32 | id | ID of the notification to check |
Returns
Type | Description |
---|---|
NotificationStatus | The status of the notification |
CreateNotificationBuilder(Int32, 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 |
---|---|---|
Int32 | 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(Int32, 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 |
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 |
---|---|
Boolean | 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.
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 |
---|---|
Int32 | 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 |
SendNotificationWithExplicitID(AndroidNotification, String, Int32)
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 |
Int32 | id | A unique ID for the notification |
UpdateScheduledNotification(Int32, 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 |
---|---|---|
Int32 | 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 |