테스트 코드와 전체 코드는 아래의 Git에서
LocalPushManager 란에서 찾아볼 수 있다.
https://github.com/OfficialCaox/UnityToolkit
GitHub - OfficialCaox/UnityToolkit: 손수 제작한 유니티 기능 모음 리포지토리(Handcrafted Unity Toolkit)
손수 제작한 유니티 기능 모음 리포지토리(Handcrafted Unity Toolkit). Contribute to OfficialCaox/UnityToolkit development by creating an account on GitHub.
github.com
본문===============================================================================
푸시 알림 관련 글 자체가 별로 없는데 IOS 최신화 버전의 코드는 거의 없어서 짧게나마 글을 남겨본다. 코드는 전부 공식 문서를 참고해서 직접 짰다.
좀 더 자세한 내용은 아래의 공식문서 참고
Namespace Unity.Notifications.Android | Mobile Notifications | 1.4.4
Namespace Unity.Notifications.Android Classes Use the AndroidNotificationCenter to register notification channels and schedule local notifications. Wrapper for the AndroidNotification. Contains the notification's id and channel. Class that queues the recei
docs.unity3d.com
유니티에서 고맙게도 자체적으로 패키지를 지원해준다. 다운로드부터 받자.
상단의 Package Manager에서 다운로드 받을 수 있다.
다운 받으면
좌측 상단 Edit > ProjectSetting > Mobile Notifications 에서 설정이 가능하다.
안드로이드 어플리케이션 세팅을 한다. 일반적인 이미지를 바로 집어넣으면 오류가 뜨는데 해당 오류는 유니티 이미지 세팅에서 Read&Write에 체크하자.
저 체크표시를 눌러주면 아이폰 앱 시작시 알림 허용 설정이 뜬다. 코드로도 구현이 가능하지만 굳이 자체 지원하는데,,, 힘 빼지 말고 체크박스 누르는 것을 추천한다.
내 게임에 적용할 알람 알고리즘을 간단히 짜보았다.
요구조건
- 메인 캐릭터의 던전 예약기능(던전을 예약하면 오프라인동안 던전을 돈다)이 오프라인 시간에 끝날 경우 사용자에게 알람을 제공해야한다.
알고리즘
1. 메인 캐릭터의 던전 클리어 예상시간을 구한다 => 2. 알람 매니저에 해당 정보를 보내 알람을 처리한다(단, 알람 매니저에서는 하나의 메서드로 Android와 IOS 푸쉬 알림을 구현해야한다. 따라서 전처리기로 처리를 한다.)
//전체 코드는 상단 Git에..
public class LocalPushManager : MonoBehaviour
{
public static void SendNotification(string title, string explain, DateTime time)
{
try
{
#if UNITY_ANDROID
var notification = new AndroidNotification();
notification.Title = title;
notification.Text = explain;
notification.FireTime = time;
notification.SmallIcon = "icon_1";
notification.LargeIcon = "icon_0";
notification.ShowInForeground = false;
string channelId = "my_channel_id";
AndroidNotificationCenter.SendNotification(notification, channelId);
#elif UNITY_IOS
var timeTrigger = new iOSNotificationTimeIntervalTrigger()
{
TimeInterval = time - DateTime.Now,
Repeats = false
};
var notification = new iOSNotification()
{
Identifier = "_notification",
Title = title,
Body = explain,
//Subtitle = explain,
ShowInForeground = false,
ForegroundPresentationOption = (PresentationOption.Alert | PresentationOption.Sound),
CategoryIdentifier = "category_a",
ThreadIdentifier = "thread1",
Trigger = timeTrigger,
};
iOSNotificationCenter.ScheduleNotification(notification);
#endif
}
catch(Exception e)
{
LogManager.AddErrorText(e.ToString());
}
}
public static void CancelAllNotifications()
{
#if UNITY_ANDROID
AndroidNotificationCenter.CancelAllNotifications();
#elif UNITY_IOS
iOSNotificationCenter.RemoveAllScheduledNotifications();
#endif
}
}
'개발일지 > 기능구현' 카테고리의 다른 글
[Unity] 투사체의 방향 전환 구현 CalculateAngle (0) | 2023.10.26 |
---|---|
[Unity] 가중치 확률 메소드 구현 WeightedRandomIndex (1) | 2023.10.26 |
[Unity] 확률 함수 구현 Random Function (1) | 2023.10.26 |
[Unity] Json 암호화 파일 Save, Load 구현 (0) | 2023.10.26 |
오프라인 보상 시스템 구현(시간 검증 포함) (0) | 2023.05.01 |