Getting Started

The User Notifications give you the ability to manage push user-facing notifications to the user's device from a server or generate them locally from your app.

Local notifications and remote notifications are ways to inform users when new data becomes available for your app, even when your app is not running in the foreground. For example, a messaging app might let the user know when a new message has arrived, and a calendar app might inform the user of an upcoming appointment. The difference between local and remote notifications is straightforward:

  • With local notifications, your app configures the notification details locally and passes those details to the system, which then handles the delivery of the notification when your app is not in the foreground. Local notifications are supported on iOS, tvOS, and watchOS.

  • With remote notifications, you use one of your company’s servers to push data to user devices via the Apple Push Notification service. Remote notifications are supported on iOS, tvOS, watchOS, and macOS.

Both local and remote notifications require you to add code to support the scheduling and handling of notifications in your app. For remote notifications, you must also provide a server environment that is capable of receiving data from user devices and sending notification-related data to the Apple Push Notification service (APNs), which is an Apple-provided service that handles the delivery of remote notifications to user devices.

When to Use Local and Remote Notifications

Because apps on iOS, tvOS, and watchOS are not always running, local notifications provide a way to alert the user when your app has new information to show. For example, an app that pulls data from a server in the background can schedule a local notification when some interesting piece of information is received. Local notifications are also well suited for apps such as calendar and to-do list apps that need to alert the user at a specific time or when a specific geographic location is reached.

Remote notifications are appropriate when some or all of the app’s data is managed by your company’s servers. With remote notifications, you decide when you want to push a notification to the user’s device. For example, a messaging app would use remote notifications to let users know when new messages arrive. Because they are sent from your server, you can send remote notifications at any time, including when the app is not running on the user’s device.

Enable User Notifications Support

When you are ready to add remove or local notifications to your application, activate the User Notifications framework, under the IOS Native Editor window. See the image below:

Note: Enabling User Notification, will also enable App Delegate.

Requesting Authorization to Interact with the User

In iOS, tvOS, and watchOS, apps must have authorization to display alerts, play sounds, or badge the app’s icon in response to incoming notifications. Requesting authorization puts control of those interactions in the hands of the user, who can grant or deny your request. The user can also change the authorization settings for your app later in the system settings.

To request authorization, call the RequestAuthorization method of the ISN_UNUserNotificationCenter class. 

If the local or remote notifications of your app interact with the user in any way, you must call this method to request authorization for those interactions. The first time your app ever calls the method, the system prompts the user to authorize the requested options. The user may respond by granting or denying authorization, and the system stores the user’s response so that subsequent calls to this method do not prompt the user again. The user may change the allowed interactions at any time. Use the GetNotificationSettings method to determine what your app is allowed to do. After determining the authorization status, the user notification center object executes the block you provide in the callback parameter. Use that block to make any adjustments in your app. For example, if authorization was denied, you might notify a remote notification server not to send notifications to the user’s device. Always call this method before scheduling any local notifications and before registering with the Apple Push Notification Service. Typically, you call this method at launch time when configuring your app's notification support. However, you may call it at another time in your app's life cycle, providing that you call it before performing any other notification-related tasks.

using SA.iOS.UserNotifications;
...
int options = ISN_UNAuthorizationOptions.Alert | ISN_UNAuthorizationOptions.Sound;
ISN_UNUserNotificationCenter.RequestAuthorization(options, (result) => {
    Debug.Log("RequestAuthorization: " + result.IsSucceeded);
});

Example of determining the authorization status:

using SA.iOS.UserNotifications;
...
ISN_UNUserNotificationCenter.GetNotificationSettings((setting) => {
    Debug.Log("Notification Settings:");
    Debug.Log(setting.AuthorizationStatus.ToString());
});

The notification settings as represented as ISN_UNNotificationSettings object.

Once your app is authorized, you may feel free to implement notifications you want.