반응형
유니티 C# 로컬 푸쉬 알림 Local Push Notification 간단 구현
에셋 다운로드
코드 작성
using UnityEngine;
using System.Collections.Generic;
using System;
using Assets.SimpleAndroidNotifications;
#if UNITY_IOS
using NotificationServices = UnityEngine.iOS.NotificationServices;
using NotificationType = UnityEngine.iOS.NotificationType;
using LocalNotification = UnityEngine.iOS.LocalNotification;
#endif
public class LocalNotificationManager : MonoBehaviour
{
string title;
string content1, content2, content3, content4;
void Start()
{
DeleteNotification();
#if UNITY_IOS
NotificationServices.RegisterForNotifications(NotificationType.Alert | NotificationType.Badge | NotificationType.Sound);
#endif
AddLocalNotification();
}
void DeleteNotification() //알람 초기화
{
#if UNITY_ANDROID
NotificationManager.CancelAll();
#elif UNITY_IOS
NotificationServices.ClearLocalNotifications();
NotificationServices.CancelAllLocalNotifications();
#endif
}
void AddLocalNotification() //알람 추가
{
title = "Game Title Name";
content1 = "TXT_CONTENT1";
content2 = "TXT_CONTENT2";
content3 = "TXT_CONTENT3";
content4 = "TXT_CONTENT4";
//오늘
DateTime dtToday = DateTime.Today;
//매일 0시에 알림
DateTime notify1 = DateTime.Now;
TimeSpan time1 = DateTime.Today.AddDays(1) - notify1;
//어플 종료 후 15분 후에 알림
DateTime notify2 = DateTime.Now.AddMinutes(1);
TimeSpan time2 = notify2 - DateTime.Now;
//어플 종료 후 알림(점심)
DateTime notify3 = Convert.ToDateTime(dtToday.ToString("yyyy/MM/dd") + " " + "12:30:00 PM");
TimeSpan time3 = notify3 - DateTime.Now;
//어플 종료 후 알림(저녁)
DateTime notify4 = Convert.ToDateTime(dtToday.ToString("yyyy/MM/dd") + " " + "7:30:00 PM");
TimeSpan time4 = notify4 - DateTime.Now;
//어플 종료 후 특정 요일에 등록 알림
DateTime dtNow = dtToday.AddDays(Convert.ToInt32(DayOfWeek.Friday) - Convert.ToInt32(dtToday.DayOfWeek));
DateTime notify5 = Convert.ToDateTime(dtNow.ToString("yyyy/MM/dd") + " " + "9:00:00 PM");
TimeSpan time5 = notify5 - DateTime.Now;
#if UNITY_ANDROID
NotificationManager.SendWithAppIcon(time1, title, content1, Color.gray, NotificationIcon.Bell);
Debug.Log("Set Android Notification");
#elif UNITY_IOS
LocalNotification noti1 = new LocalNotification();
noti1.alertTitle = title;
noti1.alertBody = content1;
noti1.soundName = LocalNotification.defaultSoundName;
noti1.applicationIconBadgeNumber = 1;
noti1.fireDate = notify1;
NotificationServices.ScheduleLocalNotification(noti1);
if (time2.Ticks > 0)
{
LocalNotification noti2 = new LocalNotification();
noti2.alertTitle = title;
noti2.alertBody = content2;
noti2.soundName = LocalNotification.defaultSoundName;
noti2.applicationIconBadgeNumber = 1;
noti2.fireDate = notify2;
NotificationServices.ScheduleLocalNotification(noti2);
}
if (time3.Ticks > 0)
{
LocalNotification noti3 = new LocalNotification();
noti3.alertTitle = title;
noti3.alertBody = content3;
noti3.soundName = LocalNotification.defaultSoundName;
noti3.applicationIconBadgeNumber = 1;
noti3.fireDate = notify3;
NotificationServices.ScheduleLocalNotification(noti3);
}
Debug.Log("Set IOS Notification");
#endif
}
}
참고할만한 글
반응형