Xamarin.iOS의 알림
Important
이 섹션의 정보는 iOS 9 및 이전과 관련이 있습니다. iOS 10 이상에서는 사용자 알림 프레임워크 가이드를 참조하세요.
iOS에는 알림이 수신되었음을 사용자에게 나타내는 세 가지 방법이 있습니다.
- 소리 또는 진동 - iOS에서 소리를 재생하여 사용자에게 알릴 수 있습니다. 소리를 사용하지 않도록 설정하면 진동하도록 디바이스를 구성할 수 있습니다.
- 경고 - 알림에 대한 정보가 포함된 대화 상자를 화면에 표시할 수 있습니다.
- 배지 - 알림이 게시되면 애플리케이션 아이콘에 숫자를 표시(배지)할 수 있습니다.
또한 iOS는 사용자에게 모든 알림(로컬 및 원격)을 표시하는 알림 센터를 제공합니다. 사용자는 화면 위쪽에서 아래로 살짝 밀어 액세스할 수 있습니다.
iOS에서 로컬 알림 만들기
iOS를 사용하면 매우 간단하게 로컬 알림을 만들고 처리할 수 있습니다. 먼저 iOS 8에서는 애플리케이션에서 알림을 표시할 수 있는 사용자의 권한을 요청해야 합니다. 로컬 알림을 보내기 전에 일반적으로 AppDelegate의 FinishedLaunching 메서드에서 다음 코드를 앱에 추가합니다.
var notificationSettings = UIUserNotificationSettings.GetSettingsForTypes(
UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, null
);
application.RegisterUserNotificationSettings(notificationSettings);
로컬 알림을 예약하려면 개체를 UILocalNotification
만들고, 개체를 FireDate
설정하고, 개체의 메서드 UIApplication.SharedApplication
를 ScheduleLocalNotification
통해 예약합니다. 다음 코드 조각은 나중에 1분 후에 발생하는 알림을 예약하고 메시지와 함께 경고를 표시하는 방법을 보여 주는 코드 조각입니다.
UILocalNotification notification = new UILocalNotification();
notification.FireDate = NSDate.FromTimeIntervalSinceNow(15);
//notification.AlertTitle = "Alert Title"; // required for Apple Watch notifications
notification.AlertAction = "View Alert";
notification.AlertBody = "Your 15 second alert has fired!";
UIApplication.SharedApplication.ScheduleLocalNotification(notification);
다음 스크린샷은 이 경고의 모양을 보여줍니다.
사용자가 알림을 허용하지 않도록 선택한 경우 아무 것도 표시되지 않습니다.
숫자로 애플리케이션 아이콘에 배지를 적용하려면 다음 줄 코드와 같이 배지를 설정할 수 있습니다.
notification.ApplicationIconBadgeNumber = 1;
아이콘으로 소리를 재생하려면 다음 코드 조각과 같이 알림에서 SoundName 속성을 설정합니다.
notification.SoundName = UILocalNotification.DefaultSoundName;
알림 소리가 30초보다 긴 경우 iOS는 대신 기본 소리를 재생합니다.
Important
iOS 시뮬레이터에는 대리자 알림을 두 번 발생시키는 버그가 있습니다. 이 문제는 디바이스에서 애플리케이션을 실행할 때 발생하지 않아야 합니다.
알림 처리
iOS 애플리케이션은 거의 동일한 방식으로 원격 및 로컬 알림을 처리합니다. 애플리케이션이 실행되면 클래스의 ReceivedLocalNotification
ReceivedRemoteNotification
메서드 또는 메서드 AppDelegate
가 호출되고 알림 정보가 매개 변수로 전달됩니다.
애플리케이션은 다양한 방법으로 알림을 처리할 수 있습니다. 예를 들어 애플리케이션은 일부 이벤트에 대해 사용자에게 알리는 경고만 표시할 수 있습니다. 또는 알림을 사용하여 서버에 파일을 동기화하는 등 프로세스가 완료되었다는 경고를 사용자에게 표시할 수 있습니다.
다음 코드는 로컬 알림을 처리하고 경고를 표시하고 배지 번호를 0으로 다시 설정하는 방법을 보여줍니다.
public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
{
// show an alert
UIAlertController okayAlertController = UIAlertController.Create(notification.AlertAction, notification.AlertBody, UIAlertControllerStyle.Alert);
okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
Window.RootViewController.PresentViewController(okayAlertController, true, null);
// reset our badge
UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
}
애플리케이션이 실행되고 있지 않으면 iOS에서 소리를 재생하거나 아이콘 배지를 해당하는 대로 업데이트합니다. 사용자가 경고와 연결된 애플리케이션을 시작하면 애플리케이션이 시작되고 FinishedLaunching
앱 대리자의 메서드가 호출되고 알림 정보가 매개 변수를 launchOptions
통해 전달됩니다. 옵션 사전에 키가 UIApplication.LaunchOptionsLocalNotificationKey
AppDelegate
포함된 경우 로컬 알림에서 애플리케이션이 시작되었음을 알 수 있습니다. 다음 코드 조각은 이 프로세스를 보여 줍니다.
// check for a local notification
if (launchOptions.ContainsKey(UIApplication.LaunchOptionsLocalNotificationKey))
{
var localNotification = launchOptions[UIApplication.LaunchOptionsLocalNotificationKey] as UILocalNotification;
if (localNotification != null)
{
UIAlertController okayAlertController = UIAlertController.Create(localNotification.AlertAction, localNotification.AlertBody, UIAlertControllerStyle.Alert);
okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
Window.RootViewController.PresentViewController(okayAlertController, true, null);
// reset our badge
UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
}
}
원격 알림 launchOptions
의 경우 연결된 연결에 원격 알림 페이로드가 포함됩니다 LaunchOptionsRemoteNotificationKey
NSDictionary
. 및 badge
sound
키를 통해 알림 페이로드를 추출할 alert
수 있습니다. 다음 코드 조각은 원격 알림을 받는 방법을 보여 줍니다.
NSDictionary remoteNotification = options[UIApplication.LaunchOptionsRemoteNotificationKey];
if(remoteNotification != null)
{
string alert = remoteNotification["alert"];
}
요약
이 섹션에서는 Xamarin.iOS에서 알림을 만들고 게시하는 방법을 보여 줬습니다. 애플리케이션에서 메서드 또는 ReceivedRemoteNotification
AppDelegate
메서드를 재정의 ReceivedLocalNotification
하 여 알림에 반응 하는 방법을 보여 줍니다.