Hanbit the Developer

Unity AndroidNotification을 이용하여, 앱 종료 후에 Notification 예약하기 본문

Game/Unity

Unity AndroidNotification을 이용하여, 앱 종료 후에 Notification 예약하기

hanbikan 2021. 5. 9. 10:47

1. 라이브러리 임포트

using Unity.Notifications.Android;

 

2. OnApplicationPause()

    private void OnApplicationPause(bool pause)
    {
        if (pause)
        {
#if UNITY_ANDROID
			//이전에 스케줄되었던 알림들 취소
            AndroidNotificationCenter.CancelAllNotifications();

            AndroidNotificationChannel notificationChannel = new AndroidNotificationChannel(
                "notification_channel1",
                "notification_title",
                "This is a notification",
                Importance.High
                );
            AndroidNotificationCenter.RegisterNotificationChannel(notificationChannel);

			//현재시간 + 1시간에 알림
            DateTime toNotify = DateTime.Now.AddHours(1);
            
            AndroidNotification notification = new AndroidNotification(
                "알림",
                "앱을 종료한 지 1시간이 되었습니다.",
                toNotify
                );

            AndroidNotificationCenter.SendNotification(notification, "notification_channel1");
#endif
        }
    }

 

- OnApplicationPause()는 앱이 종료되었을 때 호출되는 메소드이다.

- AndroidNotificationCenter.CancelAllNotifications(); 는 선택사항이다.

- AndroidNotificationChannel 클래스를 하나 만들어준다. 파라미터는 다음과 같다.

- 만든 채널을 RegisterNotificationChannel()을 통해 등록을 해준다.

- AndroidNotification 클래스로 원하는 알림을 커스터마이징해준다. 파라미터는 다음과 같다.

- 마지막으로 SendNotification()을 통해 Notification을 스케줄한다. 파라미터는 다음과 같다.

 

3. 참고 문헌

docs.unity3d.com/Packages/com.unity.mobile.notifications@1.0/api/Unity.Notifications.Android.AndroidNotification.htmldocs.unity3d.com/Packages/com.unity.mobile.notifications@1.0/api/Unity.Notifications.Android.html

'Game > Unity' 카테고리의 다른 글

Clean Code For Unity C#  (0) 2021.04.12